Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go Design Patterns

You're reading from   Go Design Patterns Best practices in software development and CSP

Arrow left icon
Product type Paperback
Published in Feb 2017
Publisher Packt
ISBN-13 9781786466204
Length 402 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Mario Castro Contreras Mario Castro Contreras
Author Profile Icon Mario Castro Contreras
Mario Castro Contreras
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Ready... Steady... Go! FREE CHAPTER 2. Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns 3. Structural Patterns - Composite, Adapter, and Bridge Design Patterns 4. Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns 5. Behavioral Patterns - Strategy, Chain of Responsibility, and Command Design Patterns 6. Behavioral Patterns - Template, Memento, and Interpreter Design Patterns 7. Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns 8. Introduction to Gos Concurrency 9. Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns 10. Concurrency Patterns - Workers Pool and Publish/Subscriber Design Patterns

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) 
    if !ok { 
        panic("Could not marshal object")  
    }  
    fmt.Println(string(jsonPackt)) 
} 
$ "pack"

First, we have defined a variable called packt to hold the contents of the packt string. Then, we have used the json library to use the Marshal command with our new variable. This will return a new bytearray with the JSON and a flag to provide and boolOK result for the operation. When we print the contents of the bytes array (previous casting to string) the expected value appears. Note that packt appeared actually between quotes as the JSON representation would be.

The encoding package

Have you realized that we have imported the package encoding/json? Why is it prefixed with the word encoding? If you take a look at Go's source code to the src/encoding folder you'll find many interesting packages for encoding/decoding such as, XML, HEX, binary, or even CSV.

Now something a bit more complicated:

type MyObject struct { 
    Number int 
    `json:"number"` 
    Word string 
} 
 
func main(){ 
    object := MyObject{5, "Packt"} 
    oJson, _ := json.Marshal(object) 
    fmt.Printf("%s\n", oJson) 
} 
$ {"Number":5,"Word":"Packt"}

Conveniently, it also works pretty well with structures but what if I want to not use uppercase in the JSON data? You can define the output/input name of the JSON in the structure declaration:

type MyObject struct { 
    Number int 
    Word string 
} 
 
func main(){ 
    object := MyObject{5, "Packt"} 
    oJson, _ := json.Marshal(object) 
    fmt.Printf("%s\n", oJson) 
} 
$ {"number":5,"string":"Packt"}

We have not only lowercased the names of the keys, but we have even changed the name of the Word key to string.

Enough of marshalling, we will receive JSON data as an array of bytes, but the process is very similar with some changes:

type MyObject struct { 
Number int`json:"number"` 
Word string`json:"string"` 
} 
 
func main(){ 
    jsonBytes := []byte(`{"number":5, "string":"Packt"}`) 
    var object MyObject 
    err := json.Unmarshal(jsonBytes, &object) 
    if err != nil { 
        panic(err) 
    } 
    fmt.Printf("Number is %d, Word is %s\n", object.Number, object.Word) 
} 

The big difference here is that you have to allocate the space for the structure first (with a zero value) and the pass the reference to the method Unmarshal so that it tries to fill it. When you use Unmarshal, the first parameter is the array of bytes that contains the JSON information while the second parameter is the reference (that's why we are using an ampersand) to the structure we want to fill. Finally, let's use a generic map[string]interface{} method to hold the content of a JSON:

type MyObject struct { 
    Number int     `json:"number"` 
    Word string    `json:"string"` 
} 
 
func main(){ 
    jsonBytes := []byte(`{"number":5, "string":"Packt"}`) 
    var dangerousObject map[string]interface{} 
    err := json.Unmarshal(jsonBytes, &dangerousObject) 
    if err != nil { 
        panic(err) 
    } 
 
    fmt.Printf("Number is %d, ", dangerousObject["number"]) 
    fmt.Printf("Word is %s\n", dangerousObject["string"]) 
    fmt.Printf("Error reference is %v\n",  
dangerousObject["nothing"])
} 
$ Number is %!d(float64=5), Word is Packt 
Error reference is <nil> 

What happened in the result? This is why we described the object as dangerous. You can point to a nil location when using this mode if you call a non-existing key in the JSON. Not only this, like in the example, it could also interpret a value as a float64 when it is simply a byte, wasting a lot of memory.

So remember to just use map[string]interface{} when you need dirty quick access to JSON data that is fairly simple and you have under control the type of scenarios described previously.

You have been reading a chapter from
Go Design Patterns
Published in: Feb 2017
Publisher: Packt
ISBN-13: 9781786466204
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime