I expected that in the code for working with struct Dish was EXPORTED as a Dish. I expected that the program would fail if the plate structure was not expanded and did not see an unexposed field in it. (OK, I could see that the region with the outstanding data is present in the EXPORTED STRUCTURE, but even this seems to be wrong).
But is the program still working as shown? How can a reflection packet see a “dish” if it is not shown?
-------------- The program is running ---------- // Modified example From the blog: http://merbist.com/2011/06/27/golang-reflection -exampl /
package main
import (
"fmt"
"reflect"
)
func main() {
for name, mtype := range attributes(&dish{}) {
fmt.Printf("Name: %s, Type: %s\n", name, mtype)
}
}
type dish struct {
Id int
last string
Name string
Origin string
Query func()
}
func attributes(m interface{}) map[string]reflect.Type {
typ := reflect.TypeOf(m)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
attrs := make(map[string]reflect.Type)
if typ.Kind() != reflect.Struct {
fmt.Printf("%v type can't have attributes inspected\n", typ.Kind())
return attrs
}
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
fmt.Println("P = ", p)
if !p.Anonymous {
attrs[p.Name] = p.Type
}
}
return attrs
}
source
share