rands

package module
v3.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 4 Imported by: 3

README

rands Build Status license codecov Go Reference

rands 为一个随机字符串生成工具。

从 v3 开始只支持 go1.22 以之后的版本

// 拿 [8,10) 数量的 rune,每个汉字都是完整的。
str := rands.String(8, 10, []rune("rands 为一个随机字符串生成工具"))

// 拿 [8,10) 数量的 byte,汉字可能会被截断。
str := rands.String(8, 10, []byte("rands 为一个随机字符串生成工具"))


// 生成一个带缓存功能的随机字符串生成器
r, err := rands.New(time.Now().Unix(), 100, 5, 7, []byte("asdfghijklmn"))
ctx,cancel := context.WithCancel(context.Background())
go r.Serve(ctx)
defer cancel()
str1 := r.String()
str2 := r.String()

安装

go get github.com/issue9/rands/v3

版权

本项目采用 MIT 开源授权许可证,完整的授权说明可在 LICENSE 文件中找到。

Documentation

Overview

Package rands 生成各种随机字符串

// 生成一个长度介于 [6,9) 之间的随机字符串
str := rands.String(6, 9, []byte("1343567"))

// 生成一个带缓存功能的随机字符串生成器
r := rands.New(nil, 100, 5, 7, []byte("adbcdefgadf;dfe1334"))
ctx,cancel := context.WithCancel(context.Background())
go r.Serve(ctx)
defer cancel()
str1 := r.String()
str2 := r.String()

NOTE: 仅是随机字符串,不保证唯一性。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alpha

func Alpha() []byte

Alpha 返回所有的字母

func AlphaNumber

func AlphaNumber() []byte

AlphaNumber Alpha + Number

func AlphaNumberPunct

func AlphaNumberPunct() []byte

AlphaNumberPunct Alpha + Number + Punct

func Bytes

func Bytes[T Char](min, max int, bs []T) []byte

Bytes 从 bs 中随机抓取 [min,max) 个字符并返回

NOTE: bs 的类型可以是 rune,但是返回类型始终是 []byte,所以用 len 判断返回值可能其值会很大。

func Number

func Number() []byte

Number 返回所有的数字

func Punct

func Punct() []byte

Punct 返回所有的标点符号

func String

func String[T Char](min, max int, bs []T) string

String 产生一个随机字符串

其长度为[min, max),bs 可用的随机字符。

Types

type Char

type Char interface{ ~byte | ~rune }

Char 约束随机字符串字符的类型

byte 的性能为好于 rune。

type Rands

type Rands[T Char] struct {
	// contains filtered or unexported fields
}

Rands 提供随机字符串的生成

func New

func New[T Char](r *rand.Rand, bufferSize, min, max int, bs []T) *Rands[T]

New 声明 Rands

如果 r 为 nil,将采用默认的随机函数; bufferSize 缓存的随机字符串数量,若为 0,表示不缓存;

func (*Rands[T]) Bytes

func (r *Rands[T]) Bytes() []byte

Bytes 产生随机字符数组

功能与全局函数 Bytes 相同,但参数通过 New 预先指定。

func (*Rands[T]) Serve

func (r *Rands[T]) Serve(ctx context.Context) error

func (*Rands[T]) String

func (r *Rands[T]) String() string

String 产生一个随机字符串

功能与全局函数 String 相同,但参数通过 New 预先指定。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL