0

I have the following structs declared in packageA

type FlagSkel struct {
    Name    string
    Short   string
    HelpMsg string
}

type FlagString struct {
    Value        string
    DefaultValue string
}

type CompositeFlagString struct {
    FlagSkel
    FlagString
}

In another package, I am trying to initialise (outside any function) a var of the later type as follows:

var Name = packageA.CompositeFlagString{
    FlagSkel: {
        Name:    "name",
        Short:   "",
        HelpMsg: "Something here",
    },
    FlagString: {
        DefaultValue: "",
    },
}

However vscode compiler shows me the attached error

enter image description here

What am I doing wrong?

2 Answers 2

1

You need to specify the type for the struct literals:

packageA.CompositeFlagString{
    FlagSkel: packageA.FlagSkel{
        Name:    "name",
        Short:   "",
        HelpMsg: "Something here",
    },
    FlagString: packageA.FlagString{
        DefaultValue: "",
    },
}
Sign up to request clarification or add additional context in comments.

Comments

0

You missed to set the type of your inner structs you want to create. your variable initialisation should be:

var Name = packageA.CompositeFlagString{
    FlagSkel: packageA.FlagSkel {
        Name:    "name",
        Short:   "",
        HelpMsg: "Something here",
    },
    FlagString: packageA.FlagString {
        DefaultValue: "",
    },
}

If you change this, it should work.

1 Comment

You are right, I didn't know this syntax. I corrected my post.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.