What version of Go are you using (go version)?
$ go version
tip, go playground.
Does this issue reproduce with the latest release?
Yes.
What did you do?
Playground link: https://play.golang.org/p/llL2iikbJmd
package main
type T struct {
GlobalName string
}
var t = T{Name: "foo"}
func (t T) Name() string {
return t.GlobalName
}
(Note: the field is called GlobalName but the composite literal specifies the incorrect field Name, which is also a method).
What did you expect to see?
An error message telling me that I'm trying to declare a composite literal field which doesn't exist (or is a method, not a struct field).
What did you see instead?
prog.go:6:11: cannot use promoted field Name in struct literal of type T
This lead to some confusion - in my case because the name of the method (Name) was similar to the name of the field I was trying to declare (GlobalName). The issue occurred because a dependency I was using changed the Name field to GlobalName and introduced an accessor method called (T).Name(). So I was in a larger process of code repair.
The struct in question also had a number of embedded fields, so it took a moment before I found the root cause (that there was Name method defined far away).
See also #23609 which improved error messages for embedding but perhaps made things more confusing in this case.
What version of Go are you using (
go version)?Does this issue reproduce with the latest release?
Yes.
What did you do?
Playground link: https://play.golang.org/p/llL2iikbJmd
(Note: the field is called
GlobalNamebut the composite literal specifies the incorrect fieldName, which is also a method).What did you expect to see?
An error message telling me that I'm trying to declare a composite literal field which doesn't exist (or is a method, not a struct field).
What did you see instead?
This lead to some confusion - in my case because the name of the method (
Name) was similar to the name of the field I was trying to declare (GlobalName). The issue occurred because a dependency I was using changed theNamefield toGlobalNameand introduced an accessor method called(T).Name(). So I was in a larger process of code repair.The struct in question also had a number of embedded fields, so it took a moment before I found the root cause (that there was
Namemethod defined far away).See also #23609 which improved error messages for embedding but perhaps made things more confusing in this case.