这是我的程序。
package main
import (
"errors"
"fmt"
)
func main() {
a := -1
err := assertPositive(a)
fmt.Printf("error: %s; int: %d\n", err, a)
fmt.Printf("error: %v; int: %d\n", err, a)
}
func assertPositive(a int) error {
if a <= 0 {
return errors.New("Assertion failure")
}
return nil
}
这是输出。
error: Assertion failure; int: -1
error: Assertion failure; int: -1
在此程序中,我使用%s
还是%v
打印error
对象都没有区别。
我有两个问题。
%s
和%v
?根据文档:
%v the value in a default format
...
%s the uninterpreted bytes of the string or slice
错误类型是接口类型。错误变量表示可以将自身描述为字符串的任何值。
因此,将其视为%s
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句