26

I'm running go vet on my CI tool, and started getting the error:

composite literal uses unkeyed fields

Because I'm instantiating

type A struct {
   *B
}

like this:

A{b} // b is of type *B

I don't care for this warning, and want to disable it on my go vet checks. How do I do this?

peterSO
  • 158,998
  • 31
  • 281
  • 276
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 3
    I think this is a bug in "go vet". There is no ambiguity in this case (only one member) and "go vet" should not report this as a problem. Same with multiple members that have incompatible types. – dolmen Jan 24 '17 at 16:57

6 Answers6

44

You can disable it or you can fix the code instead:

a := A{B: b}

playground

udondan
  • 57,263
  • 20
  • 190
  • 175
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • 25
    This is not a fix because there is no bug. This is a workaround for a bug in "go vet" that is overly religious on this matter. – dolmen Jan 24 '17 at 16:57
  • 5
    As far as I know using unkeyed fields not only just fine, but very common practice for embedded types. – theherk Jun 18 '17 at 22:57
  • 1
    I don't understand what the warning is trying to prevent. Can someone explain? Why do you need a key there? My struct only has one parameter. – Ka Mok Sep 20 '17 at 20:57
  • 15
    @KaMok mainly because say few weeks/months later you add a field to the struct, then everything that used the unkeyed initialization would break. – OneOfOne Sep 21 '17 at 20:46
  • 2
    There _are_ times where unekeyed is a good idea. If you use keyed, and a few weeks/months later you add a field to the struct, you might want your initialisation code to break to see where you need to update your initialisation code. – Jay Jul 07 '19 at 06:41
  • See also [this answer](https://stackoverflow.com/a/49461373/) for why this is the right solution. – glaux Dec 21 '22 at 10:02
23
$ go doc cmd/vet

By default all checks are performed. If any flags are explicitly set to true, only those tests are run. Conversely, if any flag is explicitly set to false, only those tests are disabled. Thus -printf=true runs the printf check, -printf=false runs all checks except the printf check.

Unkeyed composite literals

Flag: -composites

Composite struct literals that do not use the field-keyed syntax.
peterSO
  • 158,998
  • 31
  • 281
  • 276
15

If you're using the language server.

Gopls on by default in the VS Code Go extension

gopls does vet check by default.

"gopls": {
     "analyses": { "composites": false }
 },
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
5

If you are using VS code, you have to manually set the flag under settings

settings > Extensions > Go

Scroll down to "Vet Flags" section

enter image description here

Add Item and add the flag

-composites=false .

Click ok.

Save one of your files again or restart VS code to see the effect.

deepakssn
  • 5,195
  • 2
  • 24
  • 19
3

You can disable it with the -composites=false flag: e.g.,

go vet -composites=false .

NB: go tool vet is deprecated

tantrix
  • 1,248
  • 12
  • 14
2
go tool vet -composites=false .
mvndaai
  • 3,453
  • 3
  • 30
  • 34