Fetching data from map
res = map[Event_dtmReleaseDate:2009-09-15 00:00:00 +0000 +00:00 Trans_strGuestList:<nil> strID:TSTB]
How to get the following value from the above result
Event_dtmReleaseDate
strID
Trans_strGuestList
What i tried:
res.Map("Event_dtmReleaseDate");
Error : res.Map undefined (type map[string]interface {} has no field or method Map)
res.Event_dtmReleaseDate;
Error: v.id undefined (type map[string]interface {} has no field or method id)
HashMap get() Method in Java HashMap. get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.
To get the key and value elements, we should call the getKey() and getValue() methods. The Map. Entry interface contains the getKey() and getValue() methods. But, we should call the entrySet() method of Map interface to get the instance of Map.
Get Elements From a Java Map Map map = new HashMap(); map. put("key1", "value 1"); String element1 = (String) map. get("key1"); Notice that the get() method returns a Java Object , so we have to cast it to a String (because we know the value is a String).
Your variable is a map[string]interface {}
which means the key is a string but the value can be anything. In general the way to access this is:
mvVar := myMap[key].(VariableType)
Or in the case of a string value:
id := res["strID"].(string)
Note that this will panic if the type is not correct or the key does not exist in the map, but I suggest you read more about Go maps and type assertions.
Read about maps here: http://golang.org/doc/effective_go.html#maps
And about type assertions and interface conversions here: http://golang.org/doc/effective_go.html#interface_conversions
The safe way to do it without a chance to panic is something like this:
var id string var ok bool if x, found := res["strID"]; found { if id, ok = x.(string); !ok { //do whatever you want to handle errors - this means this wasn't a string } } else { //handle error - the map didn't contain this key }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With