randomstring

package module
v0.0.0-...-1374daa Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2018 License: MIT Imports: 4 Imported by: 1

README

Randomstring

Go Report Card GoDoc

A small Golang package to easely generate random strings.

The code is based on this awesome StackOverflow answer by icza.

Installation

To install randomstring, run the following command:

$ go get github.com/nouney/randomstring

Example

package main

import (
    "fmt"
    "log"

    "github.com/nouney/randomstring"
)

func main() {
    // Generate an alphanum random string of 16 chars
    str := randomstring.Generate(4)
    fmt.Println("Alphanum:", str)

    // Create a generator using digits
    rsg, err := randomstring.NewGenerator(randomstring.CharsetNum)
    if err != nil {
        log.Panic(err)
    }
    str = rsg.Generate(3)
    fmt.Println("Num:", str)

    // Create a generator using digits and lowercase alphabet
    rsg, err = randomstring.NewGenerator(randomstring.CharsetNum, randomstring.CharsetAlphaLow)
    if err != nil {
        log.Panic(err)
    }
    str = rsg.Generate(8)
    fmt.Println("Alphanumlow:", str)

    // Create a generator with a custom charset
    rsg, err = randomstring.NewGenerator("AbCdEfGhIjKlMnOpQrStUvWxYz")
    if err != nil {
        log.Panic(err)
    }
    str = rsg.Generate(18)
    fmt.Println("Custom charset:", str)
}

Default charsets

  • CharsetAlphaLow: "abcdefghijklmnopqrstuvwxyz"
  • CharsetAlphaUp: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  • CharsetNum: "0123456789"

Documentation

Overview

Package to generate random strings

Index

Constants

View Source
const (
	// [a-z]+
	CharsetAlphaLow = "abcdefghijklmnopqrstuvwxyz"
	// [A-Z]+
	CharsetAlphaUp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	// [0-9]+
	CharsetNum = "0123456789"
)

Variables

This section is empty.

Functions

func Generate

func Generate(n int) string

Generate a random string of length `n` using the default generator

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

A random string generator

func NewGenerator

func NewGenerator(charsets ...string) (*Generator, error)

Create a new generator with the given charset

func (*Generator) Generate

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

Generate a random string of length `n`

func (*Generator) WithCharsets

func (rsg *Generator) WithCharsets(cs ...string) (*Generator, error)

Change the charset of the generator

Jump to

Keyboard shortcuts

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