Skip to content

leonelquinteros/gorand

Repository files navigation

GitHub release MIT license GoDoc Build Status Go Report Card codecov

Gorand

Goodies for working with random stuff.

Package gorand defines a set of utility methods to work with random data.

The main goal is to curate a collection of functions for random data generation in different formats to be used for different purposes.

It uses the "crypto/rand" package to provide the most secure random data, unless indicated otherwise where performance can be the main focus of the method.

Most implementations are really trivial and anybody could write the same on their own packages, but here we can centralize all of them and keep a unified way of retrieving random data.

Unified QA is another motivator to have and use this package.

Below are some examples of functions inside this package, for the entire reference and docs please refer to the documentation at https://godoc.org/github.com/leonelquinteros/gorand

UUID

Generates a version 4 (randomly generated) UUID as defined in RFC 4122

package main 

import (
    "fmt"
    "github.com/leonelquinteros/gorand"
)

func main() {
    // Get UUID value
    uuid, err := gorand.UUIDv4()
    if err != nil {
        panic(err.Error())
    }

    // Marshal to string
    str, err := gorand.MarshalUUID(uuid)
	if err != nil {
		panic(err.Error())
    }
    
    // Print UUID as its canonical representation
    fmt.Println(str)
}

GetAlphaNumString

Returns a fixed length string of random letters and numbers [a-z][A-Z][0-9]

import "github.com/leonelquinteros/gorand"

func main() {
    value, err := gorand.GetAlphaNumString(24)
    if err != nil {
        panic(err.Error())
    }
    
    println(value)
}

GetHex

Retrieves a fixed amount of bytes hex number represented as a string. The result string length will have twice the number of bytes requested.

import "github.com/leonelquinteros/gorand"

func main() {
    value, err := gorand.GetHex(64)
    if err != nil {
        panic(err.Error())
    }
    
    println(value)
}