The output of this program is map[], but I want map[Id:true name:true]
I'm trying to dry up some of my SQL CRUD code and thought it would be nice to embed a persistence struct that handles reading and writing to the database. In the example below, the persistence struct would be Inner and my model would be Outer. Thanks!
http://play.golang.org/p/fsPqJ-6aLI
package main
import (
"fmt"
"reflect"
)
type Inner struct {
}
type Outer struct {
Inner
Id int
name string
}
func (i *Inner) Fields() map[string]bool {
typ := reflect.TypeOf(*i)
attrs := make(map[string]bool)
if typ.Kind() != reflect.Struct {
fmt.Printf("%v type can't have attributes inspected
", typ.Kind())
return attrs
}
// loop through the struct's fields and set the map
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
v := reflect.ValueOf(p.Type)
v = v.Elem()
attrs[p.Name] = v.CanSet()
}
}
return attrs
}
func main() {
val := Outer{}
fmt.Println(val.Fields()) // prints map[], but I want map[Id:true name:true]
}