Using gob and base64 could solve the problem, for example:
import (
"encoding/base64"
"encoding/gob"
"bytes"
)
type SX map[string]interface{}
// go binary encoder
func ToGOB64(m SX) string {
b := bytes.Buffer{}
e := gob.NewEncoder(&b)
err := e.Encode(m)
if err != nil { fmt.Println(`failed gob Encode`, err) }
return base64.StdEncoding.EncodeToString(b.Bytes())
}
// go binary decoder
func FromGOB64(str string) SX {
m := SX{}
by, err := base64.StdEncoding.DecodeString(str)
if err != nil { fmt.Println(`failed base64 Decode`, err); }
b := bytes.Buffer{}
b.Write(by)
d := gob.NewDecoder(&b)
err = d.Decode(&m)
if err != nil { fmt.Println(`failed gob Decode`, err); }
return m
}
and when you need to serialize custom struct or type (for example Session
struct), just add these lines:
func init() {
gob.Register(SX{})
gob.Register(Session{})
}
if you want to use other serialization format (2020) or this benchmark (2022) for dynamic structure