Cannot range over type interface{}

5,871 views
Skip to first unread message

Buntu Dev

unread,
Jan 14, 2014, 7:59:07 PM1/14/14
to golan...@googlegroups.com
Hi,

I'm a newbie and trying to understand how I can convert an argument of type interface {} back to a map so that I can iterate over the map.

Heres my sample code:

func SampleFunc(result interface{}) {
   // How to iterate over the result?
   // Below code gives "cannot range over result (type interface {})" error
   
    for k, v := range result {
           // do something
    }

}

func SomeOtherFunc() {
       tempMap := make(map[string]interface{})
       tempMap["aaa"] = "bbb"

       resultArray := make([]map[string]interface{}, 0)
       resultArray = append(result, rm)

       SampleFunc(resultArray)
}


How do I convert the interface back to the map array and iterate over the map to access the key-value pairs? 


Thanks for all the help!

Buntu Dev

unread,
Jan 14, 2014, 8:00:55 PM1/14/14
to golan...@googlegroups.com

There was a typo in the original message.. heres the sample code:



func SampleFunc(result interface{}) {
   // How to iterate over the result?
   // Below code gives "cannot range over result (type interface {})" error
   
    for k, v := range result {
           // do something
    }

}

func SomeOtherFunc() {
       tempMap := make(map[string]interface{})
       tempMap["aaa"] = "bbb"

       resultArray := make([]map[string]interface{}, 0)
       resultArray = append(resultArray, rm)

       SampleFunc(resultArray)
}


On Tuesday, January 14, 2014 4:59:07 PM UTC-8, Buntu Dev wrote:
Hi,

I'm a newbie and trying to understand how I can convert an argument of type interface {} back to a map so that I can iterate over the map.

Heres my sample code:

func SampleFunc(result interface{}) {
   // How to iterate over the result?
   // Below code gives "cannot range over result (type interface {})" error
   
    for k, v := range result {
           // do something
    }

}

func SomeOtherFunc() {
       tempMap := make(map[string]interface{})
       tempMap["aaa"] = "bbb"

       resultArray := make([]map[string]interface{}, 0)
       resultArray = append(resultArray, rm)

Buntu Dev

unread,
Jan 14, 2014, 8:02:08 PM1/14/14
to golan...@googlegroups.com
one more typo.. sorry:


func SampleFunc(result interface{}) {
   // How to iterate over the result?
   // Below code gives "cannot range over result (type interface {})" error
   
    for k, v := range result {
           // do something
    }

}

func SomeOtherFunc() {
       tempMap := make(map[string]interface{})
       tempMap["aaa"] = "bbb"

       resultArray := make([]map[string]interface{}, 0)
       resultArray = append(resultArray, tempMap)

       SampleFunc(resultArray)

Andy Balholm

unread,
Jan 14, 2014, 8:02:32 PM1/14/14
to golan...@googlegroups.com
Why are you passing the parameter as interface{} ?

Jsor

unread,
Jan 14, 2014, 8:06:03 PM1/14/14
to golan...@googlegroups.com
The most important question is: do you know the possible types of the map?

If so, use a type switch

switch myMap := result.(type) {
    case map[string]interface{}:
        // do stuff
    case map[int]string:
    // etc
}

If not, it gets trickier, and you have to use reflection. You'll want to take a look at reflect.ValueOf(); reflect.Value.Kind; reflect.Value.MapKeys; and reflect.Value.MapIndex.

Buntu Dev

unread,
Jan 14, 2014, 8:14:14 PM1/14/14
to golan...@googlegroups.com
Thanks Jsor for the response.. yes, I know the possible types. In the switch statement, does result get converted to a map and we can iterate over myMap? or is there any additional step to do the conversion?

Jsor

unread,
Jan 14, 2014, 8:24:22 PM1/14/14
to golan...@googlegroups.com
The myMap := result.(type) idiom in a switch statement declares myMap as a variable of the type triggered by the case, so in my example if

case map[string]interface{}

is triggered, then myMap will be a variable of type map[string]interface{}

Buntu Dev

unread,
Jan 15, 2014, 12:11:20 AM1/15/14
to golan...@googlegroups.com
Thanks Jsor, let me know if I'm over doing something:

Reply all
Reply to author
Forward
0 new messages