Documentation ¶
Overview ¶
Package utils implements misc functions to be used by all other packages.
Index ¶
- Variables
- func BigIntsFromBytes(aBytes, bBytes []byte) (*big.Int, *big.Int)
- func BigIntsFromHex(hex string) (*big.Int, *big.Int)
- func BigIntsToBytes(a, b *big.Int) []byte
- func BigIntsToHex(a, b *big.Int) string
- func BuildPort(portNum int) string
- func BytesToBigInt(bytes []byte) (*big.Int, error)
- func BytesToHex(bytes []byte) string
- func ErrorDifferent(t *testing.T, expected interface{}, result interface{})
- func FailIfDifferent(t *testing.T, expected interface{}, result interface{})
- func FromGob(target interface{}, encoded []byte)
- func FromJSON(bytes []byte, i interface{})
- func GetQuery(r *http.Request, key string) string
- func GetRoute(r *http.Request, key string) string
- func GetStrChunk(str, sep string, index int) string
- func HexHash(data interface{}) string
- func HexHashStr(data string) string
- func HexToBytes(hexStr string) []byte
- func PanicError(err error)
- func RandomAlphanum(length int) string
- func RandomHash() string
- func RandomString(cypher string, length int) string
- func Reverse(slice interface{})
- func SeedRNG()
- func ShouldPanic(t *testing.T, panicFunc func())
- func Test(t *testing.T)
- func ToGob(i interface{}) []byte
- func ToJSON(i interface{}) []byte
- func Wait()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrBigIntBadBytes = errors.New("byte slice length should equal 32")
)
Functions ¶
func BigIntsFromBytes ¶
BigIntsFromBytes decodes two big.Int pointers from a byte slice.
func BigIntsFromHex ¶
BigIntsFromHex decodes two big.Int pointers from a hexdecimal string.
func BigIntsToBytes ¶
BigIntsToBytes encodes two big.Int pointers as a byte slice.
func BigIntsToHex ¶
BigIntsToHex encodes two big.Int pointers as a hexdecimal string.
func BytesToBigInt ¶
BytesToBigInt decodes a big.Int pointer from a byte slice.
func BytesToHex ¶
BytesToHex transforms a byte slice to a hexadecimal string.
func ErrorDifferent ¶
func FailIfDifferent ¶
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 GetStrChunk ¶
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 ¶
HexHash generates a hash for any given string.
func HexToBytes ¶
HexToBytes safely transforms a hexadecimal string to a byte slice.
func RandomAlphanum ¶
RandomAlphanum generates a random alpha-numeric string.
func RandomString ¶
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 ShouldPanic ¶
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.}
Types ¶
This section is empty.