1) $ cat x.go
package main
import "reflect"
type S1 struct {
m int
}
type S2 struct{}
func (S2) m() int { return 0 }
type S struct {
S1
S2
}
func main() {
var s S
t := reflect.TypeOf(s)
if f, ok := t.FieldByName("m"); ok {
println("field found:", f.Name)
} else {
println("field not found")
}
}
2) $ go run x.go
field found: m
But in truth, the field m and the method m are at the same level and thus "cancel
each other out". In the same program, accessing s.m leads to a compile-time error (
ambiguous selector s.m ).
The bug is in http://golang.org/src/pkg/reflect/type.go?#L856
(structType.FieldByNameFunc) which ignores methods.
1) $ cat x.go package main import "reflect" type S1 struct { m int } type S2 struct{} func (S2) m() int { return 0 } type S struct { S1 S2 } func main() { var s S t := reflect.TypeOf(s) if f, ok := t.FieldByName("m"); ok { println("field found:", f.Name) } else { println("field not found") } } 2) $ go run x.go field found: m But in truth, the field m and the method m are at the same level and thus "cancel each other out". In the same program, accessing s.m leads to a compile-time error ( ambiguous selector s.m ). The bug is in http://golang.org/src/pkg/reflect/type.go?#L856 (structType.FieldByNameFunc) which ignores methods.