This code:
package main
import (
"fmt"
"encoding/json"
)
type State struct { Foo string }
type Handler struct { state State }
func (handler Handler) State() *State { return &handler.state }
func main() {
input := `{"Foo": "bar"}`
handler := Handler{}
state := handler.State()
json.Unmarshal([]byte(input), state)
fmt.Printf("%v
", state)
fmt.Printf("%v
", handler.state)
}
Prints
&{bar}
{}
This buffles me: handle.State() returns the address of handler.state,
so how is it possible that state (which is &handler.state) and handler.state end up containing different things (one is empty, the other is not)?
If I change state := handler.State() to state := &handler.state, then it works the way I expect it to.
What am I missing here?