Golang encoding: Difference between revisions
From wikinotes
No edit summary |
|||
Line 1: | Line 1: | ||
= Interface = | |||
<blockquote> | |||
The encoding libraries share a common interface, making it easy to swap between serialization formats.<br> | |||
Simply swap out the library (here <code>json</code>) for your desired output format. | |||
serialize | |||
<syntaxhighlight lang="go"> | |||
type User struct { | |||
Id: int | |||
Name: string | |||
} | |||
user := User{1, "will"} | |||
bytes, err := json.Marshall(&user) | |||
</syntaxhighlight> | |||
deserialize | |||
<syntaxhighlight lang="go"> | |||
var mapping map[string]int | |||
var serialized := []byte(`{"a": 1}`) | |||
json.Unmarshall(serialized, &mapping) | |||
</syntaxhighlight> | |||
</blockquote><!-- Interface --> | |||
== Builtin == | == Builtin == | ||
<blockquote> | <blockquote> |
Revision as of 22:21, 25 June 2022
Interface
The encoding libraries share a common interface, making it easy to swap between serialization formats.
Simply swap out the library (herejson
) 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)
Builtin
golang encoding/json golang encoding/xml golang encoding/csv