Golang encoding: Difference between revisions
From wikinotes
No edit summary |
|||
Line 1: | Line 1: | ||
= Interface = | = Interface = | ||
<blockquote> | <blockquote> | ||
The encoding libraries share a common interface, | The encoding libraries share a common interface, for serialization/deserialization.<br> | ||
Encoding-specific information is stored in struct-tags. | |||
serialize | serialize | ||
<syntaxhighlight lang="go"> | <syntaxhighlight lang="go"> | ||
type User struct { | type User struct { | ||
Id: int | Id: int `json:"id"` | ||
Name: string | Name: string `json:"name"` | ||
} | } | ||
user := User{1, "will"} | user := User{1, "will"} |
Revision as of 22:39, 25 June 2022
Interface
The encoding libraries share a common interface, for serialization/deserialization.
Encoding-specific information is stored in struct-tags.serialize
type User struct { Id: int `json:"id"` Name: string `json:"name"` } 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