首页 > 代码库 > A Tour of Go Errors
A Tour of Go Errors
An error is anything that can describe itself as an error string. The idea is captured by the predefined, built-in interface type, error
, with its single method, Error
, returning a string:
type error interface { Error() string}
The fmt
package‘s various print routines automatically know to call the method when asked to print an error
.
package main import ( "fmt" "time")type MyError struct { When time.Time What string}func (e *MyError) Error() string { return fmt.Sprintf("at %v, %s", e.When, e.What)}func run() error { return &MyError{ time.Now(), "it didn‘t work", }}func main() { if err := run(); err != nil { fmt.Println(err) }}
A Tour of Go Errors
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。