randomstring

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 4 Imported by: 2

README

Random String Generator

Random string generator, based on stack overflow answer. Depending on the length of the string can produce unique random string

Usage

x := randomstring.Generate()

For advance usage refer test file

Benchmark

Benchmark code can be found in test file

goos: linux
goarch: amd64
pkg: github.com/sabariramc/randomstring
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkGenerator/goroutines-8-8         	 8104476	       152.7 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-408-8       	 3814875	       318.4 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-808-8       	 3775564	       318.3 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-1208-8      	 4145314	       320.1 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-1608-8      	 3756034	       316.1 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-2008-8      	 3762130	       311.9 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-2408-8      	 4358134	       316.0 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-2808-8      	 4085209	       307.3 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-3208-8      	 3798603	       299.9 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-3608-8      	 4180372	       313.2 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-4008-8      	 4070395	       313.2 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-4408-8      	 4294604	       307.7 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-4808-8      	 4421068	       309.2 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-5208-8      	 4406760	       304.3 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-5608-8      	 4148553	       304.8 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-6008-8      	 3639664	       301.9 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-6408-8      	 4420321	       312.1 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-6808-8      	 3376665	       457.2 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-7208-8      	 3020142	       431.3 ns/op	      48 B/op	       2 allocs/op
BenchmarkGenerator/goroutines-7608-8      	 2765803	       433.3 ns/op	      48 B/op	       2 allocs/op

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(n int) string

Generate generates a random string of n characters using the default Generator instance.

func GenerateWithPrefix

func GenerateWithPrefix(totalLength int, prefix string) string

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

func GetLetterPool(c Config) string

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.

func (*Generator) Generate

func (r *Generator) Generate(n int) string

Generate generates a random string of n characters using the specified letter pool.

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.

Jump to

Keyboard shortcuts

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