I am a little confused with a behavior of Go regarding variable 'overriding' in embedded structure.
First situation
If a child
structure embeds a parent
structure containing a field Attr
, I can access the value of Attr indifferently with child.Attr
or child.parent.Attr
. Here is an example :
package main
import (
"fmt"
"encoding/json"
)
type parent struct {
Attr int `json:"attr"`
}
type child struct {
parent
}
func main() {
var c child
json.Unmarshal([]byte(`{"i": 1}`), &c)
fmt.Println(c.Attr)
fmt.Println(c.parent.Attr)
}
Second situation
However, if the child structure contains itself a field named Attr
, those two fields are different and can be accessed separately, as shows the following example :
package main
import (
"fmt"
"encoding/json"
)
type parent struct {
Attr int `json:"attr"`
}
type child struct {
parent
Attr int
}
func main() {
var c child
json.Unmarshal([]byte(`{"attr": 1}`), &c)
fmt.Println(c.Attr)
fmt.Println(c.parent.Attr)
}
I am very surprised that this implicit behavior is allowed in golang. I would have expected the language to be stricter as it is on so many points. Besides, I wasn't able to find a clear specification about this. Is it just a side-effect or can I use that feature?