Skip to content

Instantly share code, notes, and snippets.

@voidabhi
Created June 26, 2016 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidabhi/313583bbf503cc0bcb0bc2702922829b to your computer and use it in GitHub Desktop.
Save voidabhi/313583bbf503cc0bcb0bc2702922829b to your computer and use it in GitHub Desktop.
Golang helpers
// 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