Skip to content

Instantly share code, notes, and snippets.

@kwmt
Created October 11, 2013 16:52
Show Gist options
  • Save kwmt/6938201 to your computer and use it in GitHub Desktop.
Save kwmt/6938201 to your computer and use it in GitHub Desktop.
map -> strcut, struct -> map http://play.golang.org/p/VAaRhf-BHN <参考>http://ideone.com/XWtlo ※これはmap[string][]stringのようにvalueがスライスの場合は動かない。
package main
import (
"fmt"
r "reflect"
)
type S struct {
I int
Str string
}
type error string
func (e error) String() string { return string(e) }
func main() {
s := S{5, "Bunnies"}
m, _ := StructToMap(&s)
s1 := S{}
MapToStruct(m, &s1)
fmt.Printf("%+v\n%v\n%+v\n", s, m, s1)
}
func StructToMap(val interface{}) (mapVal map[string]interface{}, ok bool) {
structVal:= r.Indirect(r.ValueOf(val))
typ := structVal.Type()
mapVal = make(map[string]interface{})
for i := 0; i < typ.NumField(); i++ {
field := structVal.Field(i)
if field.CanSet() {
mapVal[typ.Field(i).Name] = field.Interface()
}
}
return
}
func MapToStruct(mapVal map[string]interface{}, val interface{}) (ok bool) {
structVal:= r.Indirect(r.ValueOf(val))
for name, elem := range mapVal {
structVal.FieldByName(name).Set(r.ValueOf(elem))
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment