Different behavior when printing a bytes.Buffer in Go

試著忘記壹切 提交于 2020-07-10 07:00:11

问题


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?


回答1:


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()



来源:https://stackoverflow.com/questions/53005752/different-behavior-when-printing-a-bytes-buffer-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!