Skip to content

jgweir/randstring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

randstring

randstring is a Go library to generate random strings based on a set of criteria.

Install

go get -u github.com/jgweir/randstring

Usage

To generate a random string with 8 characters:

rs := randstring.New(8)
str, err := rs.Build()

To generate a random string that only includes lowercase characters and special characters

rs := randstring.New(10)
rs.Capitals(false)
rs.Digits(false)
str, err := rs.Build()

To generate a random string with a prefix and posfix (note the total length of the string will be the number of characters in the prefix, posfix and the random string length provided)

prefix := []rune("user-")
posfix := []rune("@email.com")
rs := randstring.New(6)
rs.Capitals(false)
rs.Lowercase(true)
rs.Digits(false)
rs.Specials(true)

rs.SetPrefix(prefix)
rs.SetPosfix(posfix)
str, err := rs.Build()

To generate a random string with the swedish alphabet

cchars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ")
lchars := []rune("abcdefghijklmnopqrstuvwxyzåäö")

rs := randstring.New(6)
rs.SetCapitalChars(cchars)
rs.SetLowercaseChars(lchars)
str, err := rs.Build()

Running Example

package main

import (
    "fmt"
    "github.com/jgweir/randstring"
)

func main() {

    posfix := []rune("@email.com")

    rs := randstring.New(8)
    rs.Capitals(false)
    rs.Specials(false)
    rs.Digits(false)
    rs.SetPosfix(posfix)

    for i := 0; i < 6; i++ {
        str, _ := rs.Build()
        fmt.Println(str)
    }
}

// output
// dweuszrf@email.com
// iybiiaid@email.com
// mdifuicd@email.com
// qudcuiss@email.com
// cmiudspc@email.com
// pincissu@email.com

About

Go library for creating random strings

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages