Go 正确初始化映射[字符串]接口结构
我有以下结构:Go 正确初始化映射[字符串]接口结构,go,struct,composite-literals,Go,Struct,Composite Literals,我有以下结构: type InstructionSet struct { Inst map[string]interface{} } 在Inst地图中,我想放置如下内容 Inst["cmd"] = "dir" Inst["timeout"] = 10 现在我想直接从代码中初始化它,但我没有找到正确的方法 info := InstructionSet{ Inst: { "
type InstructionSet struct {
Inst map[string]interface{}
}
在Inst
地图中,我想放置如下内容
Inst["cmd"] = "dir"
Inst["timeout"] = 10
现在我想直接从代码中初始化它,但我没有找到正确的方法
info := InstructionSet{
Inst: {
"command": "dir",
"timeout": 10,
},
}
通过这种方式,我得到一个错误,即复合文字中缺少类型
。我尝试了一些变体,但无法找到正确的方法。错误表明复合文字中缺少类型,因此请提供类型:
info := InstructionSet{
Inst: map[string]interface{}{
"command": "dir",
"timeout": 10,
},
}
在上尝试。必须使用文字类型声明复合文字:
info := InstructionSet{
Inst: map[string]interface{}{
"command": "dir",
"timeout": 10,
},
}