utils

package
v0.0.0-...-bc1f52c Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package utils implements misc functions to be used by all other packages.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrBigIntBadBytes = errors.New("byte slice length should equal 32")
)

Functions

func BigIntsFromBytes

func BigIntsFromBytes(aBytes, bBytes []byte) (*big.Int, *big.Int)

BigIntsFromBytes decodes two big.Int pointers from a byte slice.

func BigIntsFromHex

func BigIntsFromHex(hex string) (*big.Int, *big.Int)

BigIntsFromHex decodes two big.Int pointers from a hexdecimal string.

func BigIntsToBytes

func BigIntsToBytes(a, b *big.Int) []byte

BigIntsToBytes encodes two big.Int pointers as a byte slice.

func BigIntsToHex

func BigIntsToHex(a, b *big.Int) string

BigIntsToHex encodes two big.Int pointers as a hexdecimal string.

func BuildPort

func BuildPort(portNum int) string

BuildPort buils a port string from a port number.

func BytesToBigInt

func BytesToBigInt(bytes []byte) (*big.Int, error)

BytesToBigInt decodes a big.Int pointer from a byte slice.

func BytesToHex

func BytesToHex(bytes []byte) string

BytesToHex transforms a byte slice to a hexadecimal string.

func ErrorDifferent

func ErrorDifferent(t *testing.T, expected interface{}, result interface{})

func FailIfDifferent

func FailIfDifferent(t *testing.T, expected interface{}, result interface{})

func FromGob

func FromGob(target interface{}, encoded []byte)

FromGob decodes a golang byte slice (gob) to an interface pointer.

Example
test := struct{ Test string }{Test: "I am a test struct."}
var buffer bytes.Buffer
gob.NewEncoder(&buffer).Encode(test)
encoded := buffer.Bytes()
decoded := struct{ Test string }{}
FromGob(&decoded, encoded)
fmt.Println(decoded)
Output:

{I am a test struct.}

func FromJSON

func FromJSON(bytes []byte, i interface{})

FromJSON decodes a json byte slice to an interface pointer.

Example
test := struct{ Test string }{Test: "I am a test struct."}
encoded, _ := json.Marshal(test)
decoded := struct{ Test string }{}
FromJSON(encoded, &decoded)
fmt.Println(decoded)
Output:

{I am a test struct.}

func GetQuery

func GetQuery(r *http.Request, key string) string

GetQuery gets the query parameter of index "key".

func GetRoute

func GetRoute(r *http.Request, key string) string

GetRoute gets the route parameter of index "key".

func GetStrChunk

func GetStrChunk(str, sep string, index int) string

GetStrChunk safely splits a string by an arbitrary separator and returns the chunk at position "index".

Example
test := "I;;AM;;A;;GOOFY;;;STRINGGGG"
chunk := GetStrChunk(test, ";;", 4)
fmt.Println(chunk)
Output:

;STRINGGGG

func HexHash

func HexHash(data interface{}) string

HexHash generates a hash for any given interface.

Example
test := struct{ Test string }{Test: "I am a test struct."}
hash := HexHash(test)
fmt.Println(hash)
Output:

4866e29dd676294d1920a286d872b935f7a5d7733a1b9aeecb005a882229d6f3

func HexHashStr

func HexHashStr(data string) string

HexHash generates a hash for any given string.

func HexToBytes

func HexToBytes(hexStr string) []byte

HexToBytes safely transforms a hexadecimal string to a byte slice.

func PanicError

func PanicError(err error)

PanicError panics the error unless it is nill.

func RandomAlphanum

func RandomAlphanum(length int) string

RandomAlphanum generates a random alpha-numeric string.

func RandomHash

func RandomHash() string

RandomHash generates a random lowercase hex hash.

func RandomString

func RandomString(cypher string, length int) string

RandomString generates a random string.

func Reverse

func Reverse(slice interface{})

Reverse reverses an arbitrary slice referenced by a pointer interface.

Source: https://stackoverflow.com/questions/54858529/golang-reverse-a-arbitrary-slice

func SeedRNG

func SeedRNG()

SeedRNG seeds the Random Number Generator with the current time.

func ShouldPanic

func ShouldPanic(t *testing.T, panicFunc func())

func Test

func Test(t *testing.T)

func ToGob

func ToGob(i interface{}) []byte

FromGob encodes an interface pointer to golang byte slice (gob).

Example
test := struct{ Test string }{Test: "I am a test struct."}
encoded := ToGob(test)
decoded := struct{ Test string }{}
gob.NewDecoder(bytes.NewReader(encoded)).Decode(&decoded)
fmt.Println(decoded)
Output:

{I am a test struct.}

func ToJSON

func ToJSON(i interface{}) []byte

ToJSON encodes an interface to a json byte slice.

Example
test := struct{ Test string }{Test: "I am a test struct."}
encoded := ToJSON(test)
decoded := struct{ Test string }{}
json.Unmarshal(encoded, &decoded)
fmt.Println(decoded)
Output:

{I am a test struct.}

func Wait

func Wait()

Wait pauses the current go routine until STDIN receives a new line.

Types

This section is empty.

Jump to

Keyboard shortcuts

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