为什么Error()的优先级高于String() [英] Why does Error() have priority over String()

查看:48
本文介绍了为什么Error()的优先级高于String()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找整个旅行过程,但我不知道为什么会发生这种情况.

当您有 Stringer ( String()字符串)时, fmt 将使用该方法打印到控制台.就像 https://tour.golang.org/methods/6

中建议的一样

但是,如果您添加 Error()字符串,则会调用此方法,而不是 String()字符串.

 程序包主要导入"fmt"输入Person struct {名称字符串年龄诠释}func(p * Person)String()字符串{返回fmt.Sprintf(%v(%v年)",p.名称,p.Age)}func(p * Person)Error()字符串{返回fmt.Sprintf(失败")}func main(){a:=& Person {"Arthur Dent",42}z:=& Person {"Zaphod Beeblebrox",9001}fmt.Println(a,z)} 

结果:

失败失败

我不明白为什么 fmt.Println 使用 Error 而不是 String .

解决方案

简单来说,因为这是实现方式.在实践中, error 更为重要,因此,如果实现了 error 接口,则会将其打印出来.

已记录在案,请阅读 fmt 的软件包文档:

除了使用动词%T和%p进行打印时,特殊的格式注意事项适用于实现某些接口的操作数.按申请顺序:

  1. 如果操作数实现Formatter接口,则将调用它.格式化程序可以很好地控制格式化.

  2. 如果%v动词与#标志(%#v)一起使用,并且操作数实现GoStringer接口,则会被调用.

如果该格式(对于Println等而言是隐含的%v)对于字符串(%s%q%v%x%X)有效,则以下两个规则适用:

  1. 如果操作数实现 error 接口,则将调用Error方法将对象转换为字符串,然后根据动词的要求对其进行格式化(如果有).

  2. 如果操作数实现方法 String()字符串,则将调用该方法将对象转换为字符串,然后根据动词的要求对其进行格式化(如果有)

因此,列表中的 error 是3 rd ,而 String()仅是4 .>

I've been looking through the tour for go, and I can't figure out why this happens.

When you have a Stringer (String() string), fmt will use that method for printing to console. Like suggested in https://tour.golang.org/methods/6

However if you add Error() string, this method gets called instead of String() string.

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func (p *Person) String() string {
    return fmt.Sprintf("%v (%v years)", p.Name, p.Age)
}

func (p *Person) Error() string {
    return fmt.Sprintf("Failed")
}

func main() {
    a := &Person{"Arthur Dent", 42}
    z := &Person{"Zaphod Beeblebrox", 9001}
    fmt.Println(a, z)
}

Results:

Failed Failed

I don't understand why fmt.Println uses Error instead of String.

解决方案

Simply because this is how it is implemented. An error is more important in practice, so if the error interface is implemented, that will be printed.

This is documented, read the 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 an operand implements the Formatter interface, it will be invoked. Formatter provides fine control of formatting.

  2. 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).

So error is 3rd on the list while String() is only 4th.

这篇关于为什么Error()的优先级高于String()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆