From: Shenghou Ma ByteSlice
because only *ByteSlice
satisfies io.Writer
.
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. This is because pointer methods can modify the
-receiver; invoking them on a copy of the value would cause those
-modifications to be discarded.
+invoked on pointers.
+This rule arises because pointer methods can modify the receiver; invoking
+them on a value would cause the method to receive a copy of the value, so
+any modifications would be discarded.
+The language therefore disallows this mistake.
+There is a handy exception, though. When the value is addressable, the
+language takes care of the common case of invoking a pointer method on a
+value by inserting the address operator automatically.
+In our example, the variable b
is addressable, so we can call
+its Write
method with just b.Write
. The compiler
+will rewrite that to (&b).Write
for us.
+
By the way, the idea of using Write
on a slice of bytes
is central to the implementation of bytes.Buffer
.