How to get a value from the interface?

I have a map that returns me the interface and that interface contains the pointer to the array object, so is there a way I can get data out of that array?


exampleMap := make(map[string]interface{})

I tried ranging over interface but I am getting error “cannot range over value (variable of type interface{})”

You don’t use exampleInterface anywhere. Please show code that demonstrates your problem.

I have made the change, but the interface returns me the array with pointer.

OK, now you have a map. Please show us how you try to iterate over it.


for key, value := range exampleMap  {

if key == "Struct1" {
// now here I am trying to get value which matches the key
}
}

I can’t reproduce your error. This works:

package main

import (
	"fmt"
)

func main() {
	exampleMap := make(map[string]interface{})
	exampleMap["Struct1"] = "foo"
	
	for key, value := range exampleMap {
		if key == "Struct1" {
			fmt.Printf("%s=%s\n", key, value)
		}
	}

}

Show us the code that produces your error.

1 Like