About

GolangUtilCode」🔥 是一个开发中常用的工具类。配有完整的单元测试和性能测试,不需要引用多余的库和文件,可以开箱即用。

APIs

func GetInt(max int) (int, error)  	生成个加密的安全的随机int
func GetIntInsecure(i int) int	使用当前系统时间的种子生成个随机整数
func String(n int) (string, error)  生成加密安全的字符串
func StringInsecure(n int) (string, error)	生成个密码不安全的字符串
func StringRange(min int, max int) (string, error)	生成给定范围内的安全随机字符串
func IntRange(min int, max int) (int, error)    返回给定范围之间的随机整数
func Random(n int, charset string, isSecure bool) (string, error) 从给定的字符集生成随机数据
func Bytes(n int) ([]byte, error)   生成组加密安全的字节
func Choice(j []string) (string, error)	从段字符串中随机选择
func ChoiceInsecure(j []string) string段字符串中随机选择(不安全)

ID Format

By default, the ID format follows the original Twitter snowflake format.

  • The ID as a whole is a 63 bit integer stored in an int64
  • 41 bits are used to store a timestamp with millisecond precision, using a custom epoch.
  • 10 bits are used to store a node id - a range from 0 through 1023.
  • 12 bits are used to store a sequence number - a range from 0 through 4095.

How it Works.

Each time you generate an ID, it works, like this.

  • A timestamp with millisecond precision is stored using 41 bits of the ID.
  • Then the NodeID is added in subsequent bits.
  • Then the Sequence Number is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.

The default Twitter format shown below.

+--------------------------------------------------------------------------+
| 1 Bit Unused | 41 Bit Timestamp |  10 Bit NodeID  |   12 Bit Sequence ID |
+--------------------------------------------------------------------------+

Using the default settings, this allows for 4096 unique IDs to be generated every millisecond, per Node ID.

// Create a new Node with a Node number of 1
node, err := snowflake.NewNode(1)
if err != nil {
fmt.Println(err)
return
}

// Generate a snowflake ID.
id := node.Generate()
func GenerateToken(userId, username interface{}) (string, error) 生成token
func ParseToken(strToken string) (*JwtClaims, bool)  解析token
// 核心代码
func versionOrdinal(version string) string {
	// ISO/IEC 14651:2011
	const maxByte = 1<<8 - 1

	vo := make([]byte, 0, len(version)+8)
	j := -1
	for i := 0; i < len(version); i++ {
		b := version[i]
		if '0' > b || b > '9' {
			vo = append(vo, b)
			j = -1
			continue
		}
		if j == -1 {
			vo = append(vo, 0x00)
			j = len(vo) - 1
		}
		if vo[j] == 1 && vo[j+1] == '0' {
			vo[j+1] = b
			continue
		}
		if vo[j]+1 > maxByte {
			panic("VersionOrdinal: invalid version")
		}
		vo = append(vo, b)
		vo[j]++
	}
	return string(vo)
}

iso相关」 -> 「ISO

「[ISO文档](ISO-3166 Country and Dependent Territories Lists with UN Regional Codes)」