For an example, let's take this struct
var input struct {
Title *string `json:"title"`
Year *int32 `json:"year"`
Runtime *data.Runtime `json:"runtime"`
Genres []string `json:"genres"`
}
I get the purpose of using pointer values like this when decoding JSON values. But my question is when we store a pointer to a string in the input field (for an ex) input.Title, Where does the underlying value for that pointer is stored? It's simply stored in some memory address randomly? or what actually is going on behind the scenes
CodePudding user response:
The JSON decoder calls reflect.New to get a pointer to a new empty string value. The decoder sets the value to the decoded string and sets the struct field to the pointer.
The reflect.New function calls a private runtime function to allocate memory for the value. The function returns a pointer to that memory.
