horex
horex

Reputation: 379

Different behavior when printing a bytes.Buffer in Go

When I execute this:

buf := new(bytes.Buffer)
buf.WriteString("Hello world")
fmt.Println(buf)

it prints Hello World.

But if I execute this:

var buf bytes.Buffer
buf.WriteString("Hello world")
fmt.Println(buf)

it prints: {[72 101 108 108 111 32 119 111 114 108 100] 0 [72 101 108 108 111 32 119 111 114 108 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 0}

I understand that this is the content of the structure byte.Buffer but why it is printed in a different format?

Upvotes: 4

Views: 9238

Answers (1)

icza
icza

Reputation: 418595

Because a value of type *bytes.Buffer has a String() method (the method set of *bytes.Buffer contains the String() method), and a value of type bytes.Buffer does not.

And the fmt package checks if the value being printed has a String() string method, and if so, it is called to produce the string representation of the value.

Quoting from package doc of fmt:

Except when printed using the verbs %T and %p, special formatting considerations apply for operands that implement certain interfaces. In order of application:

  1. If the operand is a reflect.Value, the operand is replaced by the concrete value that it holds, and printing continues with the next rule.

  2. If an operand implements the Formatter interface, it will be invoked. Formatter provides fine control of formatting.

  3. If the %v verb is used with the # flag (%#v) and the operand implements the GoStringer interface, that will be invoked.

If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %v %x %X), the following two rules apply:

  1. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

  2. If an operand implements method String() string, that method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

The Buffer.String() method returns its content as a string, that's what you see printed when you pass a pointer of type *bytes.Buffer. And when you pass a non-pointer value of type bytes.Buffer, it is simply printed like a normal struct value, for which the default format is:

{field0 field1 ...}

See related / similar questions:

The difference between t and *t

Why not use %v to print int and string

Why does Error() have priority over String()

Upvotes: 18

Related Questions