It's a common mistake for new users of Go* to accidentally attempt to set a field on a non-pointer receiver.
For instance:
type Something struct{
Done bool
}
func (s Something) Bar() {
if s.Done {
return
}
// Code goes here
s.Done = true
}
The above is legal in Go 1.x because s is passed by value to Bar, but the change only lasts for the duration of the function call since the local version on the stack was modified, not the value which the method was called on. This is almost always a bug and I can't think of any situations where that would be the intention of the programmer when attempting to set a property inside of a method.
This proposal is to make it illegal to modify a property on a receiver when the receiver is passed-by value. Arguments other than the receiver would unaffected.
This could be done either as ago vet check in Go 1.x, or a language change in Go2.
- I don't have any numbers to support this claim, but blaming new users means I wouldn't need to admit if it were to theoretically be a mistake that I might still occasionally make.
It's a common mistake for new users of Go* to accidentally attempt to set a field on a non-pointer receiver.
For instance:
The above is legal in Go 1.x because s is passed by value to Bar, but the change only lasts for the duration of the function call since the local version on the stack was modified, not the value which the method was called on. This is almost always a bug and I can't think of any situations where that would be the intention of the programmer when attempting to set a property inside of a method.
This proposal is to make it illegal to modify a property on a receiver when the receiver is passed-by value. Arguments other than the receiver would unaffected.
This could be done either as a
go vetcheck in Go 1.x, or a language change in Go2.