I am newer to go lang and I have a type of variable like below:
type ResultData map[string]map[string][]interface{}
When I receive data in this variable, how do I convert the whole data into single string in Go?
I am newer to go lang and I have a type of variable like below:
type ResultData map[string]map[string][]interface{}
When I receive data in this variable, how do I convert the whole data into single string in Go?
You can use something like Sprintf
:
func main() {
d1 := map[string][]interface{}{
"a": []interface{}{20, "hello"},
"b": []interface{}{100}}
d2 := map[string][]interface{}{
"x": []interface{}{"str", 10, 20},
}
m := make(map[string]map[string][]interface{})
m["d1"] = d1
m["d2"] = d2
s := fmt.Sprintf("%v", m)
fmt.Println(s)
}
Or you could also do that with the json
module to convert to a JSON string with json.Marshal
. If the actual runtime type behind your interface{}
is marshal-able to JSON, json.Marshal
will figure it out on its own.
b, _ := json.Marshal(m)
fmt.Println(string(b))