% cat /tmp/x.go
package main
type VeryLongStruct struct {
A1 int
A2 int
A3 int
A4 int
A5 int
A6 int
A7 int
A8 int
A9 int
A10 int
A11 int
A12 int
A13 int
A14 int
A15 int
A16 int
A17 int
A18 int
A19 int
A20 int
}
func main() {
var x VeryLongStruct
x.B2 = false
xx := []VeryLongStruct{{B2: false}}
_ = xx
}
% go build /tmp/x.go
# command-line-arguments
/tmp/x.go:28:4: x.B2 undefined (type VeryLongStruct has no field or method B2)
/tmp/x.go:30:26: unknown field B2 in struct literal of type struct{A1 int; A2 int; A3 int; A4 int; A5 int; A6 int; A7 int; A8 int; A9 int; A10 int; A11 int; A12 int; A13 int; A14 int; A15 int; A16 int; A17 int; A18 int; A19 int; A20 int}
%
Note that the plain x.B2 assignment prints a nice error mentioning VeryLongStruct by name.
In contrast, the same assignment in the struct literal prints the actual struct definition, which is too long to be useful. Saying
/tmp/x.go:30:26: unknown field B2 in struct literal of type VeryLongStruct
would be better.
/cc @griesemer
Comment From: gabyhelp
Similar Issues
- cmd/compile: improve error message for "too few values in struct literal" #51877
- cmd/compile: improve compiler error on embedded structs #23609
- cmd/gofmt: formatting of struct literal depends inconsistently on lengths of field names #7335
- cmd/compile: 1.7 compiler does not compile program with large complex struct #18920
- cmd/compile: incorrect column reported for invalid struct field names #24339
- cmd/compile: confusing compiler errors for bad method name on named struct literal #38745
- cmd/compile: better error message for wrong-cased field name in composite literal #22794
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: gopherbot
Change https://go.dev/cl/595075 mentions this issue: go/types, types2: report type name in comp. literal error, if possible