Published on

Marshaling and Unmarshaling in Go

Authors
  • avatar
    Name
    Michael H
    Twitter

Tutorial for marshaling and unmarshaling in Go, focusing on both JSON and XML data formats.

Marshaling and Unmarshaling in Go

Marshaling is the process of converting a Go object into a data format such as JSON or XML. Unmarshaling is the reverse process, converting data from formats like JSON or XML back into a Go object.

1. JSON Marshaling and Unmarshaling

Import the necessary package

import (
    "encoding/json"
    "fmt"
)

Define a Go struct

type Person struct {
    Name   string `json:"name"`
    Age    int    `json:"age"`
    Email  string `json:"email"`
}

Marshaling a Go struct to JSON

func main() {
    person := Person{
        Name:  "John Doe",
        Age:   30,
        Email: "[email protected]",
    }

    jsonData, err := json.Marshal(person)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(string(jsonData))
}

Unmarshaling JSON to a Go struct

func main() {
    jsonString := `{"name":"Jane Doe","age":25,"email":"[email protected]"}`

    var person Person
    err := json.Unmarshal([]byte(jsonString), &person)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Printf("%+v\n", person)
}

2. XML Marshaling and Unmarshaling

Import the necessary package

import (
    "encoding/xml"
    "fmt"
)

Define a Go struct

type Person struct {
    Name   string `xml:"name"`
    Age    int    `xml:"age"`
    Email  string `xml:"email"`
}

Marshaling a Go struct to XML

func main() {
    person := Person{
        Name:  "John Doe",
        Age:   30,
        Email: "[email protected]",
    }

    xmlData, err := xml.Marshal(person)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(string(xmlData))
}

Unmarshaling XML to a Go struct

func main() {
    xmlString := `<Person><name>Jane Doe</name><age>25</age><email>[email protected]</email></Person>`

    var person Person
    err := xml.Unmarshal([]byte(xmlString), &person)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Printf("%+v\n", person)
}

Handling Nested Structures

If your JSON or XML data contains nested objects, you need to define nested structs in Go.

JSON Example with Nested Structs

type Address struct {
    Street string `json:"street"`
    City   string `json:"city"`
}

type Person struct {
    Name    string  `json:"name"`
    Age     int     `json:"age"`
    Email   string  `json:"email"`
    Address Address `json:"address"`
}

func main() {
    jsonString := `{"name":"Jane Doe","age":25,"email":"[email protected]","address":{"street":"123 Main St","city":"Somewhere"}}`

    var person Person
    err := json.Unmarshal([]byte(jsonString), &person)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Printf("%+v\n", person)
}

XML Example with Nested Structs

type Address struct {
    Street string `xml:"street"`
    City   string `xml:"city"`
}

type Person struct {
    Name    string  `xml:"name"`
    Age     int     `xml:"age"`
    Email   string  `xml:"email"`
    Address Address `xml:"address"`
}

func main() {
    xmlString := `<Person><name>Jane Doe</name><age>25</age><email>[email protected]</email><address><street>123 Main St</street><city>Somewhere</city></address></Person>`

    var person Person
    err := xml.Unmarshal([]byte(xmlString), &person)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Printf("%+v\n", person)
}