Home > Backend Development > Golang > Why Does `json.Unmarshal` Fail with a Nil Pointer to Primitive Types?

Why Does `json.Unmarshal` Fail with a Nil Pointer to Primitive Types?

DDD
Release: 2024-11-22 08:50:19
Original
654 people have browsed it

Why Does `json.Unmarshal` Fail with a Nil Pointer to Primitive Types?

Unmarshaling JSON: Pointers vs. References

The json.Unmarshal function allows us to convert JSON data into a Go struct. While the documentation suggests that it can allocate a new value if the pointer is nil, this is not the case.

Consider the following example:

var animals Animal

err := json.Unmarshal(jsonBlob, &animals)
Copy after login

This works as expected because animals is a non-nil pointer. However, if we change animals to an uninitialized pointer:

var animals *Animal

err := json.Unmarshal(jsonBlob, animals)
Copy after login

This fails with the obscure error message:

json: Unmarshal(nil *main.Animal)
Copy after login

This error stems from an InvalidUnmarshalError, which is thrown when an invalid argument is passed to Unmarshal. The documentation does not explicitly state that the pointer must be non-nil, which could lead to confusion.

The conflicting quote in the documentation, which suggests that Unmarshal can allocate a new value for nil pointers, may be referring to the behavior of the reflect.New function, which is used internally by Unmarshal to create a new value when the pointer is nil. However, reflect.New only works for certain types, such as structs and slices. It does not work for pointers to primitive types, such as *int or *string.

In summary, json.Unmarshal requires a non-nil pointer as its second argument. It cannot allocate a new value for nil pointers of primitive types. Therefore, it is important to ensure that the pointer is non-nil before attempting to Unmarshal JSON data into it.

The above is the detailed content of Why Does `json.Unmarshal` Fail with a Nil Pointer to Primitive Types?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template