參考https://studygolang.com/pkgdocgolang
導入方式:函數
import "errors"
errors包實現了建立錯誤值的函數。spa
1)func Newcode
func New(text string) error
使用字符串建立一個錯誤,請類比fmt包的Errorf方法,差很少能夠認爲是New(fmt.Sprintf(...))。blog
舉例:字符串
package main import( "errors" "fmt" ) func main() { const name, id = "coco", 17 err := errors.New(fmt.Sprintf("user %q (id %d )not found", name, id)) if err != nil{ fmt.Println(err) } }
返回:string
userdeMBP:go-learning user$ go run test.go user "coco" (id 17 )not found
等價於:class
package main import( "fmt" ) func main() { const name, id = "coco", 17 err := fmt.Errorf("user %q (id %d )not found", name, id) if err != nil{ fmt.Println(err) } }
返回結果相同test