Go JSON Unmarshalling: Structs vs. Interfaces – When to Use Pointers?

Patricia Arquette
Release: 2024-11-26 20:11:11
Original
127 people have browsed it

Go JSON Unmarshalling: Structs vs. Interfaces – When to Use Pointers?

Unmarshalling JSON into Structs vs. Interfaces

In Go, the encoding/json package provides functionality for converting JSON data to and from Go values. When unmarshalling JSON into a struct, it's important to understand that the package requires explicit instructions on the target type.

By default, when unmarshalling JSON into an interface, the package selects an appropriate type based on the data structure. For JSON objects, it chooses map[string]interface{}, and for JSON arrays, it selects []interface{}. However, this behavior differs when dealing with structs.

To unmarshal JSON into a specific struct, you need to pass a pointer to the struct as the second parameter of json.Unmarshal(). This is because Go automatically dereferences pointers when passing them to functions.

However, if you attempt to unmarshal JSON into an interface{} variable containing a non-pointer struct value, the package has no way of determining the intended target type. Instead, it will interpret the data as a map or slice, depending on its structure.

To resolve this issue, you should wrap your struct value in an interface{} using a pointer reference. This ensures that json.Unmarshal has the necessary information to unmarshal into the appropriate type:

func getFoo() interface{} {
    return &Foo{"bar"} // Use a pointer wrapper
}
Copy after login

Here's an improved version of the example code:

package main

import (
    "encoding/json"
    "fmt"
)

type Foo struct {
    Bar string `json:"bar"`
}  

func getFoo() interface{} {
    return &Foo{"bar"}
}

func main() {

    fooInterface := getFoo() 

    myJSON := `{"bar":"This is the new value of bar"}`
    jsonBytes := []byte(myJSON)

    err := json.Unmarshal(jsonBytes, fooInterface )
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("%T %+v", fooInterface, fooInterface) 
}
Copy after login

This updated code will successfully print the unmarshaled Foo struct with the updated "bar" value.

The above is the detailed content of Go JSON Unmarshalling: Structs vs. Interfaces – When to Use Pointers?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template