Golang encoding

From wikinotes
Revision as of 22:21, 25 June 2022 by Will (talk | contribs)

Interface

The encoding libraries share a common interface, making it easy to swap between serialization formats.
Simply swap out the library (here json) for your desired output format.

serialize

type User struct {
    Id:   int
    Name: string
}
user := User{1, "will"}
bytes, err := json.Marshall(&user)

deserialize

var mapping map[string]int
var serialized := []byte(`{"a": 1}`)
json.Unmarshall(serialized, &mapping)

Libraries

Builtin

golang encoding/json
golang encoding/xml
golang encoding/csv