/randstr

A module that contains functions for generating random strings.

Primary LanguageGoMIT LicenseMIT

randstr

randstr is a module that contains functions for generating random strings.

The functions in this module uses the crypto/rand package.

Installation

go get github.com/henrikac/randstr

Usage

The strings generated by Generate and GenerateLen are all unbiased, meaning that each of the characters in the character set a-zA-Z0-9 (62 characters) has an equal chance of being used in the generated string.

Generate

randstr.Generate returns a random string that is 16 characters long.

package main

import (
	"fmt"
	"log"

	"github.com/henrikac/randstr"
)

func main() {
	str, err := randstr.Generate()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", str)
}

// Example output:
// 4a1Lz3d4yXihvbJX

GenerateLen

randstr.GenerateLen returns a random string of the specified length.

package main

import (
	"fmt"
	"log"

	"github.com/henrikac/randstr"
)

func main() {
	str, err := randstr.GenerateLen(20)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", str)
}

// Example output:
// bRXVtwgaLVsBuYJtP9Lf

Base64Encoded

randstr.Base64Encoded returns a random base64 encoded string.

package main

import (
	"fmt"
	"log"

	"github.com/henrikac/randstr"
)

func main() {
	str, err := randstr.Base64Encoded()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", str)
}

// Example output:
// M72WQsavclA/wLYfxuyr2Q==

Contributing

  1. Fork it (https://github.com/henrikac/randstr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors