The analyzer has detected a method that modifies the receiver field passed by value. Such changes affect only a structure copy and do not affect the original object.
The example:
type Buffer struct {
data []byte
}
func (b Buffer) Append(value byte) {
b.data = append(b.data, value)
}
The Append method is declared with the Buffer receiver, which is passed by value. A copy of the object is created each time the method is called, so the b.data = append(b.data, value) expression modifies only the field of that copy. After the method execution is complete, the original object remains unchanged.
If a method is supposed to modify an object, use a pointer receiver:
func (b *Buffer) Append(value byte) {
b.data = append(b.data, value)
}
Was this page helpful?