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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.