Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate generates a random string of n characters using the default Generator instance.
func GenerateWithPrefix ¶
GenerateWithPrefix generates a random string with the specified prefix and total length.
Example ¶
package main import ( "fmt" "regexp" "github.com/sabariramc/randomstring" ) func main() { x := randomstring.GenerateWithPrefix(20, "cust_") fmt.Println(len(x)) match, _ := regexp.MatchString("^cust_[a-zA-Z0-9]{15}$", x) fmt.Println(match) }
Output: 20 true
func GetLetterPool ¶
GetLetterPool returns the letter pool based on the provided configuration.
Types ¶
type Config ¶ added in v1.1.1
type Config struct { Int bool // Int indicates whether to include integers in the generated string. UpperCase bool // UpperCase indicates whether to include uppercase letters in the generated string. LowerCase bool // LowerCase indicates whether to include lowercase letters in the generated string. }
Config represents the configuration options for generating random strings.
func GetLetterPoolConfig ¶
func GetLetterPoolConfig(options ...LetterPoolOption) Config
GetLetterPoolConfig returns the configuration for the letter pool based on the provided options.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator generates random strings based on the specified letter pool.
Example ¶
package main import ( "fmt" "regexp" "github.com/sabariramc/randomstring" ) func main() { gen := randomstring.NewGenerator() x := gen.Generate(10) match, _ := regexp.MatchString("^[a-zA-Z0-9]{10}$", x) fmt.Println(match) }
Output: true
Example (Onlylowercase) ¶
package main import ( "fmt" "regexp" "github.com/sabariramc/randomstring" ) func main() { gen := randomstring.NewGenerator(randomstring.WithoutUpperCase(), randomstring.WithoutInt()) x := gen.Generate(10) match, _ := regexp.MatchString("^[a-z]{10}$", x) fmt.Println(match) }
Output: true
Example (Onlynumerals) ¶
package main import ( "fmt" "regexp" "github.com/sabariramc/randomstring" ) func main() { gen := randomstring.NewGenerator(randomstring.WithoutLowerCase(), randomstring.WithoutUpperCase()) x := gen.Generate(10) match, _ := regexp.MatchString("^[0-9]{10}$", x) fmt.Println(match) }
Output: true
Example (Onlyuppercase) ¶
package main import ( "fmt" "regexp" "github.com/sabariramc/randomstring" ) func main() { gen := randomstring.NewGenerator(randomstring.WithoutLowerCase(), randomstring.WithoutInt()) x := gen.Generate(10) match, _ := regexp.MatchString("^[A-Z]{10}$", x) fmt.Println(match) }
Output: true
func NewGenerator ¶
func NewGenerator(options ...LetterPoolOption) *Generator
NewGenerator creates a new Generator instance with the specified options for generating random strings.
type LetterPoolOption ¶ added in v1.1.0
type LetterPoolOption func(*Config)
LetterPoolOption represents functional options for configuring the letter pool for generating random strings.
func WithoutInt ¶
func WithoutInt() LetterPoolOption
WithoutInt excludes integers from the letter pool when generating random strings.
func WithoutLowerCase ¶
func WithoutLowerCase() LetterPoolOption
WithoutLowerCase excludes lowercase letters from the letter pool when generating random strings.
func WithoutUpperCase ¶
func WithoutUpperCase() LetterPoolOption
WithoutUpperCase excludes uppercase letters from the letter pool when generating random strings.