# Random

random 随机数生成器包,可以生成随机[]bytes, int, string。

# 源码:

# 用法:

import (
    "github.com/duke-git/lancet/v2/random"
)

# 文档

# RandBytes

生成随机字节切片

函数签名:

func RandBytes(length int) []byte

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
	randBytes := random.RandBytes(4)
	fmt.Println(randBytes)
}

# RandInt

生成随机int, 范围[min, max)

函数签名:

func RandInt(min, max int) int

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
	rInt := random.RandInt(1, 10)
	fmt.Println(rInt)
}

# RandInt

生成随机给定长度的随机字符串

函数签名:

func RandString(length int) string

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
	randStr := random.RandString(6)
	fmt.Println(randStr)
}

# UUIdV4

生成UUID v4字符串

函数签名:

func UUIdV4() (string, error)

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/random"
)

func main() {
	uuid, err := random.UUIdV4()
    if err != nil {
        return
    }
	fmt.Println(uuid)
}
最后更新时间: 2022/9/29 下午1:53:14