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 }
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) }
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!