Managing JSON data
JSON is the acronym for JavaScript Object Notation and, like the name implies, it's natively JavaScript. It has become very popular and it's the most used format for communication today. Go has very good support for JSON serialization/deserialization with the JSON
package that does most of the dirty work for you. First of all, there are two concepts to learn when working with JSON:
Marshal: When you marshal an instance of a structure or object, you are converting it to its JSON counterpart.
Unmarshal: When you are unmarshaling some data, in the form of an array of bytes, you are trying to convert some JSON-expected-data to a known struct or object. You can also unmarshal to a
map[string]interface{}
in a fast but not very safe way to interpret the data as we'll see now.
Let's see an example of marshaling a string:
import ( "encoding/json" "fmt" ) func main(){ packt := "packt" jsonPackt, ok := json.Marshal(packt) ...