没有,为什么要这样?
=== update ===
package main
import (
"encoding/json"
"fmt"
"reflect"
)
type Foo struct {
Bar string
Baz int
}
func main() {
fooType := reflect.TypeOf((*Foo)(nil)).Elem()
var fields []reflect.StructField
for i := 0; i < fooType.NumField(); i++ {
field := fooType.Field(i)
field.Type = reflect.PtrTo(field.Type)
fields = append(fields, field)
}
fooTypeForUpdate := reflect.StructOf(fields)
j := []byte(`{
"Baz": 9
}`)
data := reflect.New(fooTypeForUpdate)
if err := json.Unmarshal(j, data.Interface()); err != nil {
panic(err)
}
data = data.Elem()
for i := 0; i < data.NumField(); i++ {
field := data.Field(i)
t := fooType.Field(i)
if field.IsNil() {
continue
}
field = field.Elem()
fmt.Printf("%s: %v\n", t.Name, field.Interface())
}
}