Skip to content
/ randstr Public

Customizable random string generator in Go/Golang.

License

Notifications You must be signed in to change notification settings

ihexxa/randstr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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