gorand

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2019 License: MIT Imports: 8 Imported by: 10 modules, 13 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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)
}

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAlphaNumString

func GetAlphaNumString(n int) (string, error)

GetAlphaNumString returns a fixed length (n int) string of random letters and numbers [a-z][A-Z][0-9]

func GetAlphaString

func GetAlphaString(n int) (string, error)

GetAlphaString returns a fixed length (n int) string of random letters [a-z][A-Z]

func GetBytes

func GetBytes(n int) ([]byte, error)

GetBytes returns a fixed amount of random bytes. Specify the amount of bytes wanted by passing it on the n parameter. This function is the base for most of the methods on this package.

func GetHex

func GetHex(n int) (string, error)

GetHex returns a string representation of a (n) bytes random hexadecimal number. The length of the result string will be twice as long as the amount of bytes requested due to hex representation.

func GetNumString

func GetNumString(n int) (string, error)

GetNumString returns a fixed length (n int) string of random numbers [0-9]

func GetRandomChars

func GetRandomChars(c string, n int) (string, error)

GetRandomChars returns a fixed length string (n int) of chars contained on a collection string provided (c string).

func ID

func ID() (id [64]byte, err error)

ID generates a [64]byte random value, using time and local identifier into it.

First (most-significative) 15 bytes: time value Next 9 bytes: Local process randomly-generated identifier Next 40 bytes: Random value

func MarshalUUID

func MarshalUUID(uuid UUID) (s string, err error)

MarshalUUID converts UUID into its canonical string representation. It doesn't check for version or varian bits, so it can be used with invalid (non RFC 4122 compilant) values.

Types

type UUID

type UUID [16]byte

UUID as defined in RFC 4122

func UUIDv4

func UUIDv4() (uuid UUID, err error)

UUIDv4 generates a version 4 (randomly generated) UUID

func UnmarshalUUID

func UnmarshalUUID(s string) (uuid UUID, err error)

UnmarshalUUID parses a string representation of a UUID and returns its []bytes value. It doesn't check for version or varian bits, so it can be used with invalid (non RFC 4122 compilant) values.

Jump to

Keyboard shortcuts

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