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