dongmeiran609914 2018-10-26 09:35
浏览 33
已采纳

打印字节时的不同行为

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条回答 默认 最新

  • doujianqin5172 2018-10-26 09:41
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端