Encountered such a problem, expect to make a general method to update the value in the map, as in the above example:
type Student struct {
Name string `json:"name"`
}
func TestReflect(t *testing.T) {
a := make(map[int]Student)
update(&a)
fmt.Println("outer:",a)
}
func update(a interface{}) {
b := copyStruct(a)
valueStr := "{\"0\":{\"name\":\"test\"}}"
_ = json.Unmarshal([]byte(valueStr), &b)
fmt.Println("inner:",b)
a = b
}
func copyStruct(a interface{}) interface{} {
// Create a pointer type to the underlying element type
return reflect.New(reflect.TypeOf(a).Elem())
}
The expected output result is: inner: map[0:map[name:test]] outer: map[0:map[name:test]]
But the actual output is : inner: map[0:map[name:test]] outer: map[]
How can i do .... Can somebody help
Aucun commentaire:
Enregistrer un commentaire