Golang slicing tool class

Time:2021-7-13
package services

import (
	crand "crypto/rand"
	"errors"
	"fmt"
	"github.com/astaxie/beego/orm"
	"io"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

type UtilService struct {
}

//uuid
func (this *UtilService) Uuid() (string, error) {
	uuid := make([]byte, 16)
	n, err := io.ReadFull(crand.Reader, uuid)
	if n != len(uuid) || err != nil {
		return "", err
	}

	uuid[8] = uuid[8]&^0xc0 | 0x80
	uuid[6] = uuid[6]&^0xf0 | 0x40
	return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:9], uuid[9:12], uuid[12:15]), nil
}

//Random scrambling of strings
func (this *UtilService) RandomString(length int) string {
	//48 ~ 57 digits
	// 65 ~ 90 A ~ Z
	// 97 ~ 122 a ~ z
	//A total of 62 characters, random in 0 ~ 61, less than 10, random in the number range,
	//Less than 36 is random in upper case, others are random in lower case
	rand.Seed(time.Now().UnixNano())
	result := make([]string, 0, length)
	for i := 0; i < length; i++ {
		t := rand.Intn(62)
		if t < 10 {
			result = append(result, strconv.Itoa(t))
		} else if t < 36 {
			result = append(result, string(rand.Intn(26)+65))
		} else {
			result = append(result, string(rand.Intn(26)+97))
		}
	}
	return strings.Join(result, "")
}

//The slices were randomly scrambled
func (this *UtilService) RandomSlice(strings []orm.Params) ([]orm.Params, error) {

	if len(strings) <= 0 {
		return []orm.Params{}, errors.New("the length of the parameter strings should not be less than 0")
	}

	for i := len(strings) - 1; i > 0; i-- {
		num := rand.Intn(i + 1)
		strings[i], strings[num] = strings[num], strings[i]
	}

	return strings, nil
}

//The slices were randomly scrambled
func (this *UtilService) RandomSlice2(stringss *[]interface{}) {

	for i := len(*stringss) - 1; i > 0; i-- {
		num := rand.Intn(i + 1)
		(*stringss)[i], (*stringss)[num] = (*stringss)[num], (*stringss)[i]
	}

}



//Intersection of two slices
func (this *UtilService) SliceIntersect(s1 []string, s2 []string) []string {
	s := make([]string, 0)
	for _, v1 := range s1 {
		for _, v2 := range s2 {
			if v1 == v2 {
				s = append(s, v1)
			}
		}
	}
	return s
}

//Difference set of two slices
func (this *UtilService) SliceDiff(s1 []string, s2 []string) []string {
	s := make([]string, 0)
	for _, v1 := range s1 {

		if !this.InArray(s2, v1) {
			s = append(s, v1)
		}

	}
	return s
}

//Determine whether there is an element in the slice
func (this *UtilService) InArray(s1 []string, val string) bool {
	for _, v := range s1 {
		if v == val {
			return true
		}
	}
	return false
}