Skip to content

Latest commit

 

History

History
63 lines (41 loc) · 1.61 KB

README.md

File metadata and controls

63 lines (41 loc) · 1.61 KB

randstr

Build Status Go Report PkgGoDev

Customizable random string generator in Go/Golang.

Examples

Document is here.

Note: This library should NOT be used in security-critical system.

Generate random strings with numbers/alphabets/alnums/candidates:

import "github.com/ihexxa/randstr"

// customized generator
candidates := []string{"你", "好", "世", "界"}
randStr := randstr.New(candidates)

fmt.Println(randStr.Gen()) // output could be: 世世界你你你你好
fmt.Println(randStr.Alphabets())
fmt.Println(randStr.Numbers())
fmt.Println(randStr.Alnums())

Generate fixed-length random strings:

import "github.com/ihexxa/randstr"

candidates := []string{"你", "好", "世", "界"}
randStr := randstr.New(candidates)
randStr.SetLenFixed(true) // set sample size fixed
randStr.SetLenMax(8) // set sample size to 8
// above 3 lines can be replaced with randstr.NewRandstr(candidates, true, 8)

fmt.Println(randStr.Gen())
...

Generate random slices:

import "github.com/ihexxa/randstr"

randStr := randstr.New([]string{"你", "好", "世", "界"})

fmt.Println(randStr.GenSlice())
fmt.Println(randStr.AlphabetSlice())
fmt.Println(randStr.NumberSlice())
fmt.Println(randStr.AlnumSlice())

License

MIT