如何从地图中获取价值
从地图中获取数据
res = map[Event_dtmReleaseDate:2009-09-15 00:00:00 +0000 +00:00 Trans_strGuestList:<nil> strID:TSTB]
注意
如何从上述结果中获取以下值
Note
How to get the following value from the above result
1.Event_dtmReleaseDate
1.Event_dtmReleaseDate
2.strID
3.Trans_strGuestList
3.Trans_strGuestList
我尝试过的事情:
- res.Map("Event_dtmReleaseDate");
错误:res.Map未定义(类型map [string] interface {}没有字段或方法Map)
Error : res.Map undefined (type map[string]interface {} has no field or method Map)
- res.Event_dtmReleaseDate;
错误:v.id未定义(类型map [string] interface {}没有字段或方法ID)
Error: v.id undefined (type map[string]interface {} has no field or method id)
您的变量是map[string]interface {}
,表示键是字符串,但值可以是任何值.通常,访问此文件的方式是:
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)
请注意,如果类型不正确或键在映射中不存在,这将会引起恐慌,但是我建议您阅读更多有关Go映射和类型断言的信息.
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.
在此处阅读有关地图的信息: http://golang.org/doc/effective_go.html#maps
Read about maps here: http://golang.org/doc/effective_go.html#maps
有关类型声明和接口转换的信息,请参见: http://golang.org/doc/effective_go.html#interface_conversions
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
}