user2969402
user2969402

Reputation: 1251

Go vet: "composite literal uses unkeyed fields" with embedded types

I have a simple structure:

type MyWriter struct {
    io.Writer
}

Which I then use in the following way:

writer = MyWriter{io.Stdout}

When running go vet this gives me a composite literal uses unkeyed fields.

In order to fix this would I have to turn io.Reader into a field in the MyWriter structure by adding a key?

type MyWriter struct {
    w io.Writer
}

Is there any other way around this? The only other answer I found on here suggests to disable the check altogether but I would rather not do that and find a proper solution.

Upvotes: 10

Views: 9205

Answers (1)

Andrew Hare
Andrew Hare

Reputation: 351516

Try this:

writer = MyWriter{Writer: io.Stdout}

Embedded structs have an implicit key of the type name itself without the package prefix (e.g. in this case, Writer).

Upvotes: 33

Related Questions