golang 转换为字符串
In Golang, fmt.Println(err)
can print out the error err
. But how to convert an error to a string explicitely?
在Golang中, fmt.Println(err)
可以打印出错误 err
。 但是如何将错误显式转换为字符串 ?
In Go, the error
type has an interface
在Go中, error
类型具有接口
type error interface {
Error() string
}
Ref: https://golang.org/pkg/builtin/#error
参考: https : //golang.org/pkg/builtin/#error
So, for an error err
, you can call
因此,对于错误err
,您可以致电
err.Error()
to get a string representing err
.
得到一个表示err
的字符串。
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/in-golang-how-to-convert-an-error-to-a-string/
golang 转换为字符串