10

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.

user2969402
  • 1,221
  • 3
  • 16
  • 26

1 Answers1

33

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635