Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

warnings

go

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.

like image 529
user2969402 Avatar asked Mar 24 '18 04:03

user2969402


1 Answers

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).

like image 197
Andrew Hare Avatar answered Nov 05 '22 13:11

Andrew Hare