GoLang生成不重复六位验证码

不同的整数生成固定六位的字符串验证码

package main

import (
    "container/list"
    "fmt"
)

var baseStr string = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
var base []byte = []byte(baseStr)

func Base34(n uint64) []byte {
    quotient := n
    mod := uint64(0)
    l := list.New()
    for quotient != 0 {
        mod = quotient % 34
        quotient = quotient / 34
        l.PushFront(base[int(mod)])
    }
    listLen := l.Len()
    if listLen >= 6 {
        res := make([]byte, 0, listLen)
        for i := l.Front(); i != nil; i = i.Next() {
            res = append(res, i.Value.(byte))
        }
        return res
    } else {
        res := make([]byte, 0, 6)
        for i := 0; i < 6; i++ {
            if i < 6-listLen {
                res = append(res, base[0])
            } else {
                res = append(res, l.Front().Value.(byte))
                l.Remove(l.Front())
            }

        }
        return res
    }

}

func main() {
    res := Base34(25226)
    fmt.Println("===============base:25226->", string(res))

    res = Base34(4204052)
    fmt.Println("===============base:4204052->", string(res))
    res = Base34(1544513516)
    fmt.Println("===============base:1544513516->", string(res))
}

欢迎关注【技术型项目经理】公众号。可获取软件行业动态、技术积累和项目管理理念文章分享。选择「考试经验」菜单「PMP」、「高项」(信息系统项目管理师)、「CISSP」、「GoLang」可获取学习资料。

你可能感兴趣的:(GoLang生成不重复六位验证码)