How to get a value from map

huangapple go评论84阅读模式
英文:

How to get a value from map

问题

问题

从地图中获取数据

数据格式

res = map[Event_dtmReleaseDate:2009-09-15 00:00:00 +0000 +00:00 Trans_strGuestList:<nil> strID:TSTB]

注意

如何从上述结果中获取以下值

  1. Event_dtmReleaseDate
  2. strID
  3. Trans_strGuestList

我尝试过的方法:

  1. res.Map("Event_dtmReleaseDate");

> 错误:res.Map未定义(类型map[string]interface {}没有字段或方法Map)

  1. res.Event_dtmReleaseDate;

> 错误:v.id未定义(类型map[string]interface {}没有字段或方法id)

英文:

Problem

Fetching data from map

Data Format

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
  2. strID
  3. Trans_strGuestList

What i tried:

  1. res.Map("Event_dtmReleaseDate");

> Error : res.Map undefined (type map[string]interface {} has no field or method Map)

  1. res.Event_dtmReleaseDate;

> Error: v.id undefined (type map[string]interface {} has no field or method id)

答案1

得分: 142

你的变量是一个 map[string]interface {},这意味着键是字符串,但值可以是任何类型。一般来说,访问它的方式是:

mvVar := myMap[key].(VariableType)

或者在值为字符串的情况下:

id := res["strID"].(string)

请注意,如果类型不正确或键在映射中不存在,这将引发 panic 错误。我建议你阅读更多关于 Go 中的映射和类型断言的内容。

在这里了解关于映射的更多信息:http://golang.org/doc/effective_go.html#maps

以及关于类型断言和接口转换的内容:http://golang.org/doc/effective_go.html#interface_conversions

如果你想要安全地进行操作而不会引发 panic 错误,可以像这样处理:

var id string
var ok bool
if x, found := res["strID"]; found {
     if id, ok = x.(string); !ok {
        // 处理错误的逻辑 - 这意味着这不是一个字符串类型
     }
} else {
   // 处理错误的逻辑 - 映射中不包含该键
}
英文:

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
}

答案2

得分: 17

一般来说,要从映射中获取值,你需要像这样做:

package main

import "fmt"

func main() {
    m := map[string]string{"foo": "bar"}
    value, exists := m["foo"]
    // 如果键不存在于映射中,exists 变量的值将为 false。
    fmt.Printf("映射中存在键:%t,值:%v \n", exists, value)
}

结果将是:

映射中存在键:true,值:bar
英文:

In general to get value from map you have to do something like this:

package main

import "fmt"

func main() {
	m := map[string]string{"foo": "bar"}
	value, exists := m["foo"]
    // In case when key is not present in map variable exists will be false.
	fmt.Printf("key exists in map: %t, value: %v \n", exists, value)
}

Result will be:

key exists in map: true, value: bar

huangapple
  • 本文由 发表于 2014年12月18日 19:15:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/27545270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定