receive a binary json message from a channel in go

Assuming you have a channel ch that receives the binary JSON message, you can use the encoding/json package to unmarshal the binary data into a Go struct.

main.go
type Message struct {
    Field1 string
    Field2 int
    // ...
}

for {
    binaryMsg := <-ch
    var msg Message
    err := json.Unmarshal(binaryMsg, &msg)
    if err != nil {
        // handle error
    }
    // handle the message
}
238 chars
16 lines

In the example above, a struct Message is defined to represent the structure of the JSON data. The binary data is read from the channel and unmarshalled into an instance of Message. If the unmarshalling fails, the error should be handled appropriately.

gistlibby LogSnag