Pointers vs. Values
methods can be defined for any named type (except a pointer or an interface)
value receiver
type ByteSlice []byte
func (slice ByteSlice) Append(data []byte) []byte {
// ...
}
pointer receiver
func (p *ByteSlice) Append(data []byte) {
slice := *p
// Body as above, without the return.
*p = slice
}
The rule about pointers vs. values for receivers is that
- value methods can be invoked on pointers and values,
- but pointer methods can only be invoked on pointers.