Created
June 26, 2016 22:25
-
-
Save voidabhi/313583bbf503cc0bcb0bc2702922829b to your computer and use it in GitHub Desktop.
Golang helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GetBytes accepts any object (interface{]}) and returns its json encoded byte array | |
func GetBytes(key interface{}) ([]byte) { | |
var buf bytes.Buffer | |
enc := json.NewEncoder(&buf) | |
err := enc.Encode(key) | |
if err != nil { | |
return []byte("") | |
} | |
return buf.Bytes() | |
} | |
// GetBytes accepts any object (interface{]}) and returns its json encoded string | |
func GetString(key interface{}) string { | |
return string(GetBytes(key)[:]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment