Skip to content

Instantly share code, notes, and snippets.

@viiftw
Forked from miguelmota/interface_to_bytes.go
Created April 19, 2021 08:51
Show Gist options
  • Save viiftw/4f7b7d64869b492c7a37a833499601b2 to your computer and use it in GitHub Desktop.
Save viiftw/4f7b7d64869b492c7a37a833499601b2 to your computer and use it in GitHub Desktop.
Golang interface to bytes using gob encoder
package main
import (
"encoding/gob"
"bytes"
)
func GetBytes(key interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(key)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment