英文:
How to create Python like dictionaries with Go?
问题
我正在尝试创建一个类似Python的字典。我已经尝试了以下代码:
var chunk = map[string]string{
"code": "5000",
"error": err,
}
var payload = map[string]string{
"type": "response",
"error": chunk,
}
我还尝试了以下代码:
var payload = map[string]string{
"type": "response",
"error": {
"code": "5000",
"error": err,
},
}
请注意,以上代码是使用类似Python的语法示例,并不是真正的Python代码。
英文:
I'm trying to make a python-like dictionary. I've tried:
var chunk = map[string]string{
"code": "5000",
"error": err,
}
var payload = map[string]string{
"type": "response",
"error": chunk,
}
I've also tried
var payload = map[string]string{
"type": "response",
"error": {
"code": "5000",
"error": err,
},
}
答案1
得分: 14
Go是一种静态类型语言,因此无法创建动态映射。你将映射定义为map[string]string
,所以映射的键和值必须是字符串类型。
你可以使用map[string]interface{}
来动态使用映射。但是,每当你使用值时,你需要将其转换为你所需的类型。
例如:
chunk := map[string]interface{}{
"code": "5000",
"error": err,
"a": 5,
"b": 7,
}
intvalue := chunk["a"].(int)
英文:
Go is a statically typed language. So you can not create dynamic maps. You defined your maps as map[string]string
so your map key and values needs to be string.
You can use map[string]interface{}
to use a map dynamically. But whenever you use the value you need to cast it in your desired type.
As example
chunk := map[string]interface{}{
"code": "5000",
"error": err,
"a": 5,
"b": 7,
}
intvalue := chunk["a"].(int)
答案2
得分: 3
@sadlil 给出了一个正确的答案。但是我想再添加一个额外的情况。
在Python中,一个字典也可以是这样的:
dict = {"key1": "value", 1: "one", "key2": 2, (1,2,3): False}
在同一个字典中,不仅值可以是不同类型的,键也可以是不同类型的。
如果你总是需要一个字符串键,那么 map[string]interface{}
可以给出适当的答案。
但是,为了使字典更像Python风格,你也可以这样做:
map[interface{}]interface{}
示例:
type AB struct {
data string
}
var ab = AB{data: "data"}
var pyLikeDict = map[interface{}]interface{}{
"key1": "value",
1: "one",
"key2": 2,
true: "true",
ab: false,
}
for key, value := range pyLikeDict {
fmt.Printf("%v -> %v\n", key, value)
}
// key1 -> value
// 1 -> one
// key2 -> 2
// true -> true
// {data} -> false
在[kbd]playground中查看。
英文:
@sadlil gave a correct answer. But I want to add one additional case.
In python, a dictionary can be like this too
dict = {"key1":"value", 1: "one", "key2": 2, (1,2,3): False}
where not only value, but also key can be different type in a same dictionary.
If you always need a string key, this map[string]interface{}
give appropriate answer.
But, to make more python-like-dictionary, you can also do this
map[interface{}]interface{}
Example
type AB struct {
data string
}
var ab = AB{data: "data"}
var pyLikeDict = map[interface{}]interface{}{
"key1": "value",
1: "one",
"key2": 2,
true: "true",
ab: false,
}
for key, value := range pyLikeDict {
fmt.Printf("%v -> %v\n", key, value)
}
// key1 -> value
// 1 -> one
// key2 -> 2
// true -> true
// {data} -> false
See in <kbd>playground</kbd>
答案3
得分: 0
你正在创建的地图是map[string]string。你可能想要使用map[string]interface{}来指定通用指针类型。
请注意,你需要一些类型转换和语法糖来强制转换*chunk和其中的字符串。试着调试一下,你会弄明白的
英文:
The maps you're creating are map[string]string. You probably want map[string]interface{} instead to specify generic pointer types.
Note that you'll need some casts and syntactic sugar to coerce the *chunk and strings in there. Play with it a bit--you'll figure it out
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论