iterating over map[string] interface{}

8,693 views
Skip to first unread message

DEXTER

unread,
Apr 10, 2012, 12:07:16 PM4/10/12
to golang-nuts
Hi
How do I iterate over a map[string] interface{}

I can access the interface map value & type, when the map string is
known to me
arg1 := reflect.ValueOf(response["response"])
arg1 = reflect.Indirect(arg1)
if arg1.IsValid() {
if arg1.Type().String() == "string" {
str += arg1.String()
}
}

I would like to access the map name, its type

Dexter

Gustavo Niemeyer

unread,
Apr 10, 2012, 12:11:13 PM4/10/12
to DEXTER, golang-nuts
Hi there,

> How do I iterate over a map[string] interface{}

It's a normal map, and you don't need reflection to iterate over it or
consider the value type. For example:

for key, value := range yourMap {
if s, ok := value.(string); ok {
fmt.Printf("%q is a string: %q\n", key, s)
}
}

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

DEXTER MCCONNELL

unread,
Apr 10, 2012, 12:18:03 PM4/10/12
to Gustavo Niemeyer, golang-nuts
How do I access the map when it contains an array of strings ?

map["response"] = "Blah Blah...."
map["items"] = make([]string, 0)

I am be to access the values of response, how do I access the value of items ?
--
...at the entrance gate of Heaven!

Gustavo Niemeyer

unread,
Apr 10, 2012, 12:23:26 PM4/10/12
to DEXTER MCCONNELL, golang-nuts
On Tue, Apr 10, 2012 at 13:18, DEXTER MCCONNELL <djc...@gmail.com> wrote:
> How do I access the map when it contains an array of strings ?
>
> map["response"] = "Blah Blah...."
> map["items"] = make([]string, 0)
>
> I am be to access the values of response, how do I access the value of items?

Same way:

if items, ok := map["items"].([]string) {
...

DEXTER MCCONNELL

unread,
Apr 10, 2012, 1:18:24 PM4/10/12
to Gustavo Niemeyer, golang-nuts
I got this error
When I accessed using
if items, ok := response["items"].([]string) {
   ...
}


items, ok = response["items"].([]string) used as value


On Tue, Apr 10, 2012 at 9:53 PM, Gustavo Niemeyer <gus...@niemeyer.net> wrote:
On Tue, Apr 10, 2012 at 13:18, DEXTER MCCONNELL <djc...@gmail.com> wrote:
> How do I access the map when it contains an array of strings ?
>
> map["response"] = "Blah Blah...."
> map["items"] = make([]string, 0)
>
> I am be to access the values of response, how do I access the value of items?

Same way:

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog

-- I'm not absolutely sure of anything.

DEXTER MCCONNELL

unread,
Apr 10, 2012, 1:22:15 PM4/10/12
to Gustavo Niemeyer, golang-nuts
BTW my map is  return value from a function

response := MyFunc("groups")
response["response"] = string
response["items"] = make([]string,0)

Gustavo Niemeyer

unread,
Apr 10, 2012, 1:22:48 PM4/10/12
to DEXTER MCCONNELL, golang-nuts
On Tue, Apr 10, 2012 at 14:18, DEXTER MCCONNELL <djc...@gmail.com> wrote:
> I got this error
> When I accessed using
> if items, ok := response["items"].([]string) {

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

Matt Kane's Brain

unread,
Apr 10, 2012, 1:23:15 PM4/10/12
to DEXTER MCCONNELL, Gustavo Niemeyer, golang-nuts
Try:

if items, ok := response["items"].([]string); ok {
   ...
}

Assignments in go don't have a value and can't be tested.

On Tue, Apr 10, 2012 at 13:18, DEXTER MCCONNELL <djc...@gmail.com> wrote:
> I got this error
> When I accessed using
> if items, ok := response["items"].([]string) {
>    ...
> }
>
>
> items, ok = response["items"].([]string) used as value


--
matt kane's brain
http://hydrogenproject.com

DEXTER MCCONNELL

unread,
Apr 10, 2012, 1:30:23 PM4/10/12
to Gustavo Niemeyer, golang-nuts
Thanks this worked

items,ok := response["items"].([]string)
Reply all
Reply to author
Forward
0 new messages