-2

below is my code

func main() {
    var a interface{}
    b := make(map[string]interface{})
    b["mac_addr"] = "fa:16:3e:ba:95:bd"
    b["type"] = "fixed"
    b["addr"] = "1.1.1.1"
    a = b
    fmt.Println(a)
}

a gives me output map[addr:1.1.1.1 mac_addr:fa:16:3e:ba:95:bd type:fixed]

issue is how do I access value of addr from a

  • 2
    `a.(map[string]interface{})["addr"]` – blackgreen Nov 28 '21 at 07:34
  • thanks, yes it did work, i was able to do get the required value. But I am getting another type, below is the structure, how do I get the value from this, I am getting ```Addresses: (map[string]interface {}) (len=1) { (string) (len=7) "network": ([]interface {}) (len=1 cap=1) { (map[string]interface {}) (len=2) { (string) (len=15) "type": (string) (len=5) "fixed", (string) (len=4) "addr": (string) (len=7) "1.1.1.1" } } }``` – Anoop Sharda Nov 28 '21 at 08:32
  • managed to get this working. here is the working solution. ``` for _, y := range Addresses { for _, z := range y.(interface{}).([]interface{}) { log.Println(z.(map[string]interface{})["addr"].(string)) } } ``` – Anoop Sharda Nov 28 '21 at 08:37

1 Answers1

0

use assert

val,ok:=a.(map[string]interface{})

if ok {

fmt.println(val["addr"])

}