正确初始化map[string]接口结构



我有以下结构:

type InstructionSet struct {
Inst map[string]interface{}
}

Inst地图中,我想放一些类似的东西

Inst["cmd"] = "dir"
Inst["timeout"] = 10

现在我想直接从代码中初始化它,但我没有找到正确的方法来完成

info := InstructionSet{
Inst: {
"command": "dir",
"timeout": 10,
},
}

通过这种方式,我得到了一个错误,说missing type in composite literal。我尝试了一些变体,但找不到合适的方法。

错误表明复合文字中缺少类型,因此请提供类型:

info := InstructionSet{
Inst: map[string]interface{}{
"command": "dir",
"timeout": 10,
},
}

在围棋场上试试吧。

必须使用文字的类型声明复合文字:

info := InstructionSet{
Inst: map[string]interface{}{
"command": "dir",
"timeout": 10,
},
}

最新更新