1
0
mirror of https://github.com/golang/go synced 2024-10-01 16:08:33 -06:00
go/pointer/testdata/structreflect.go
Alan Donovan 7a70c382be go.tools/pointer: fix panic in reflection.
reflect.Values may point to tagged objects with
interface type, e.g. x := reflect.ValueOf(new(interface{})).Elem().
We failed to consider this when implementing Elem.

Also, (reflect.Value).Interface() must do one "unboxing"
when it encounters such tagged objects.
i.e., x.Elem().Interface() and x.Interface() are equivalent
in that case.

Also:
- add example of tagged object with interface type.
- untabify (Label).String docstring.
- added tests.

R=crawshaw
CC=golang-dev
https://golang.org/cl/18020044
2013-10-28 10:58:46 -04:00

46 lines
1016 B
Go

// +build ignore
package main
import "reflect"
type A struct {
f *int
g interface{}
h bool
}
var dyn string
func reflectTypeFieldByName() {
f, _ := reflect.TypeOf(A{}).FieldByName("f")
print(f.Type) // @pointsto *int
g, _ := reflect.TypeOf(A{}).FieldByName("g")
print(g.Type) // @pointsto interface{}
print(reflect.Zero(g.Type)) // @pointsto <alloc in reflect.Zero>
print(reflect.Zero(g.Type)) // @types interface{}
print(reflect.Zero(g.Type).Interface()) // @pointsto
print(reflect.Zero(g.Type).Interface()) // @types
h, _ := reflect.TypeOf(A{}).FieldByName("h")
print(h.Type) // @pointsto bool
missing, _ := reflect.TypeOf(A{}).FieldByName("missing")
print(missing.Type) // @pointsto
dyn, _ := reflect.TypeOf(A{}).FieldByName(dyn)
print(dyn.Type) // @pointsto *int | bool | interface{}
}
func reflectTypeField() {
fld := reflect.TypeOf(A{}).Field(0)
print(fld.Type) // @pointsto *int | bool | interface{}
}
func main() {
reflectTypeFieldByName()
reflectTypeField()
}