4

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
surendra
  • 121
  • 1
  • 1
  • 7

1 Answers1

6

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))
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412