2

I have the following struct:

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

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.

icza
  • 389,944
  • 63
  • 907
  • 827
cips
  • 95
  • 1
  • 8

2 Answers2

5

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,
    },
}

Try it on the Go Playground.

icza
  • 389,944
  • 63
  • 907
  • 827
2

Composite literals have to be declared using the type of the literal:

info := InstructionSet{
        Inst: map[string]interface{}{
            "command": "dir",
            "timeout": 10,
            },
    }
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59