I have the follow whole codes:
I hope that string convert map in golang, and use golang reflect.
The following code have simpled from my project.
package main
import (
"encoding/json"
"fmt"
"reflect"
)
func main() {
jsonStr := `{"name": "thinkerou", "age": 31, "balance": 3.14}`
var a map[string]interface{}
var value reflect.Value = reflect.ValueOf(&a)
// call function and pass param
f(jsonStr, value)
// print result
fmt.Println(value.Kind(), value.Interface())
}
func f(v string, value reflect.Value) {
personMap := make(map[string]interface{})
err := json.Unmarshal([]byte(v), &personMap)
if err != nil {
panic(err)
}
value = reflect.Indirect(value)
value = reflect.MakeMap(value.Type())
for k, v := range personMap {
// set key/value
value.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
// print result
fmt.Println(value.Kind(), value.Interface())
}
and run it and will get the result:
map map[age:31 balance:3.14 name:thinkerou]
ptr &map[]
I hope the follow result:
map map[age:31 balance:3.14 name:thinkerou]
map map[age:31 balance:3.14 name:thinkerou]
How should I pass reflect.Value
param? thanks!