Skip to content

Repository files navigation

go-randomstring

Fast, flexible random string generation for Go — ids, tokens, and passwords in one small, dependency-free package.

Go Reference CI License: MIT

Features

  • 🔤 Flexible character sets — seven built-in universes, or bring your own (Unicode included).
  • 🔒 Crypto-secure mode — draw from crypto/rand for passwords and security tokens.
  • 🔁 Unique batches — generate thousands of guaranteed-distinct strings in one call.
  • 🎲 Deterministic output — pin a Seed for reproducible results in tests.
  • 🚀 Fast — an ASCII fast path with zero dependencies beyond the standard library.

Installation

go get github.com/gbbocchini/go-randomstring

Quick start

package main

import (
	"fmt"

	"github.com/gbbocchini/go-randomstring"
)

func main() {
	r := randomstring.Randomizer{
		Universe: randomstring.LowerUpperDigits,
		Length:   13,
	}
	id, err := r.GenerateOne()
	if err != nil {
		panic(err)
	}
	fmt.Println(id)
}

Usage

Basic generation

r := randomstring.Randomizer{
	Universe: randomstring.LowerLetters,
	Length:   8,
}
id, err := r.GenerateOne()

Unique batches

Set Unique to guarantee every string in a batch is distinct:

r := randomstring.Randomizer{
	Universe: randomstring.LowerUpperDigits,
	Length:   8,
	Unique:   true,
}
ids, err := r.Generate(10_000) // 10,000 distinct ids

Crypto-secure passwords

r := randomstring.Randomizer{
	Universe: randomstring.LowerUpperDigitsSymbols,
	Length:   32,
	Secure:   true,
}
password, err := r.GenerateOne()

Deterministic output (great for tests)

r := randomstring.Randomizer{
	Universe: randomstring.LowerUpperDigits,
	Length:   13,
	Seed:     1,
}
id, _ := r.GenerateOne() // always "9g14r5YgIsx9v"

Custom universe

r := randomstring.Randomizer{
	Universe: "ABC123", // only these characters
	Length:   6,
}

Built-in character sets

Constant Contents
LowerLetters a-z
UpperLetters A-Z
Digits 0-9
Symbols !@#$%&*()-_+={};:.,
LowerUpperLetters a-z + A-Z
LowerUpperDigits a-z + A-Z + 0-9
LowerUpperDigitsSymbols a-z + A-Z + 0-9 + symbols

Documentation

Full API reference is available on pkg.go.dev.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change, and update tests as appropriate.

License

MIT © Gabriel Bocchini

About

A GoLang based string randomizer. It uses runes to generate random strings that can be used as ids, passwords, etc

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages