Hits

go 工具方法

go 工具方法

// Go生成随机特权码
func GenerateCode(randomLength, randomNum int) []string {
    privilegeArr := make([]string, randomNum)
    privilegeTmp := make([]rune, randomLength)
    var letters = []rune("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789")
    randSeed := rand.New(rand.NewSource(time.Now().UnixNano()))
    for i := 0; i < randomNum; i++ {
        for j := 0; j < randomLength; j++ {
            privilegeTmp[j] = letters[randSeed.Intn(len(letters))]
        }
        fmt.Println(string(privilegeTmp))
        privilegeArr[i] = string(privilegeTmp)
    }
    return privilegeArr
}

// 替换手机号中间4位为*
func ReplacePhone(phone string) string {
    old := ""
    for k, v := range phone{
        if k >=3 && k <=6{
            old = old + string(v)
        }
    }
    phone = strings.Replace(phone,old,"****",-1)
    return phone
}

func RelacePhone(phone string) string {
    // phone := "13412345678"
    slice := strings.Split(phone, "")
    str := strings.Join(slice[0:3], "") + "****" + strings.Join(slice[7:], "")
    fmt.Println(str)
    fmt.Println(phone[:3] + "****" + phone[len(phone)-4:])
    fmt.Println(phone[:3] + "****" + phone[7:])
   return str
}

本文链接:参与评论 »

--EOF--

专题「跟我一起学Go」的其它文章 »

Comments