正确初始化一个 map[string]interface 结构 [英] Properly initialize a map[string]interface struct
本文介绍了正确初始化一个 map[string]interface 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下结构:
type InstructionSet struct {
Inst map[string]interface{}
}
在 Inst
地图中,我想放一些类似
In the Inst
map I'd like to put something like
Inst["cmd"] = "dir"
Inst["timeout"] = 10
现在我想直接从代码初始化它,但我没有找到正确的方法
Now I'd like to initialize it directly from code, but I'm not finding the proper way to do it
info := InstructionSet{
Inst: {
"command": "dir",
"timeout": 10,
},
}
通过这种方式,我收到一个错误消息,说复合文字中缺少类型
.我尝试了一些变体,但我无法找到正确的方法.
In this way I get an error saying missing type in composite literal
. I tried few variations, but I can't get the proper way.
推荐答案
错误提示复合文字中缺少类型,因此请提供类型:
The error says the type is missing in the composite literal, so do provide the type:
info := InstructionSet{
Inst: map[string]interface{}{
"command": "dir",
"timeout": 10,
},
}
在 Go Playground 上试试.
这篇关于正确初始化一个 map[string]interface 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文