問題
マップからデータを取得しています
データ形式
res = map[Event_dtmReleaseDate:2009-09-15 00:00:00 +0000 +00:00 Trans_strGuestList:<nil> strID:TSTB]
注記
上記の結果から次の値を取得する方法
Event_dtmReleaseDate
strID
Trans_strGuestList
試したこと:
res.Map("Event_dtmReleaseDate");
エラー: res.Map が未定義です (型 map[string]interface {} にフィールドまたはメソッド Map がありません)
res.Event_dtmReleaseDate;
エラー: v.id が未定義です (type map[string]interface {} にフィールドまたはメソッド ID がありません)
ベストアンサー1
変数は です。map[string]interface {}
つまり、キーは文字列ですが、値は何でもかまいません。一般的に、これにアクセスする方法は次のとおりです。
mvVar := myMap[key].(VariableType)
または文字列値の場合:
id := res["strID"].(string)
型が正しくないか、キーがマップ内に存在しない場合はパニックになることに注意してください。Go マップと型アサーションについてさらに詳しく読むことをお勧めします。
マップについてはこちらをご覧ください:http://golang.org/doc/effective_go.html#マップ
型アサーションとインターフェース変換については、こちらをご覧ください。http://golang.org/doc/effective_go.html#インターフェース変換
パニックに陥ることなく安全に行う方法は次のとおりです。
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
}