2

I'm trying to create a map with an array containing maps. My code:

Go:

func main() {
    m := map[string][]map[string]string{
        "photos": [{"a":"1"}, {"b": "2"}],
        "pictures": [{"a":"1"}, {"b": "2"}]
        }
    fmt.Println(m)
}

What is wrong with this? Is this possible?

http://play.golang.org/p/UkokGzvtGL

1 Answer 1

7
  1. No Square brackets
  2. Always comma at the end of the line

http://play.golang.org/p/USm9kqDANn

package main

import "fmt"

func main() {
    m := map[string][]map[string]string{
        "photos":   {{"a": "1"}, {"b": "2"}},
        "pictures": {{"a": "1"}, {"b": "2"}},
    }
    fmt.Println(m)
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. I'm actually trying to obtain a value from a flickr api json response. the result looks like this: { "photos": { "page": 1, "pages": "107553", "photo": [ { "id": "15035296541", "owner": "51816274@N05",}]}. how do I access id?
like this: result["photos"]["photo"][0]["id"]
thanks. when I do that, I get a panic error saying the index is out of range. I did: res := make(map[string]map[string][]map[string]string); json.Unmarshal(body, &res); photourl, _ := res["photos"]["photo"][0]["url_m"]
then it means that "photo" array has no values. You should check the len() of the array before accessing it
You're really better off using a struct: play.golang.org/p/vRRZc8L5rL -- not sure why everyone tries to do this the hard way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.