12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package lib
- import (
- "crypto/md5"
- "encoding/hex"
- "math/rand"
- "strconv"
- "strings"
- "time"
- )
- // #取得随机字符串:通过打乱slice来操作
- func GetRandstring(length int, char string, rand_x int64) string {
- if length < 1 {
- return ""
- }
- if len(char) <= 6 || len(char) <= length {
- char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- }
- charArr := strings.Split(char, "")
- ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
- l := len(charArr)
- for i := l - 1; i > 0; i-- {
- r := ran.Intn(i)
- charArr[r], charArr[i] = charArr[i], charArr[r]
- }
- rchar := charArr[:length]
- return strings.Join(rchar, "")
- }
- // 返回一个32位md5加密后的字符串
- func Md5(str string) string {
- h := md5.New()
- h.Write([]byte(str))
- return hex.EncodeToString(h.Sum(nil))
- }
- // 转化
- func TimeStrToTime(T_time string) (time.Time, bool) {
- stamp, err := time.Parse("2006-01-02 15:04:05", T_time)
- if err != nil {
- return time.Time{}, false
- }
- return stamp, true
- }
- func SplitStringIds(str string, prefix string) (r []string) {
- Ids_str := strings.TrimRight(str, "|")
- Ids := strings.Split(Ids_str, "|")
- for _, v := range Ids {
- r = append(r, strings.TrimLeft(v, prefix))
- }
- return r
- }
- func SplitStringToDotStr(str string, prefix string) (r string) {
- Ids_str := strings.TrimRight(str, "|")
- Ids := strings.Split(Ids_str, "|")
- for _, v := range Ids {
- r += strings.TrimLeft(v, prefix) + ","
- }
- r = strings.TrimRight(r, ",")
- return r
- }
- func SplitStringToIntIds(str string, prefix string) (r []int) {
- Ids_str := strings.TrimRight(str, "|")
- Ids := strings.Split(Ids_str, "|")
- for _, v := range Ids {
- id, _ := strconv.Atoi(strings.TrimLeft(v, prefix))
- r = append(r, id)
- }
- return r
- }
- func IntIdsDistinct(Ids []int) (result []int) {
- distinctMap := make(map[int]int, len(Ids))
- for _, Id := range Ids {
- if _, ok := distinctMap[Id]; !ok {
- distinctMap[Id] = 1
- result = append(result, Id)
- }
- }
- return result
- }
|