This is a creation in Article, where the information may have evolved or changed.
Error
errors are anything you can describe yourself with a string. The main idea is to return the string by the predefined built -in interface type ' Error ', and the method ' error ':
Type Error Interface { error () string}
fmt
This method is called automatically when you output one of several different print functions for a package error
.
Here is an example
the basic data type of package Mainimport ("FMT") type myfloat float64 //go cannot directly increment functions, only first define a own data type, func (Data myfloat) Error () string{ return FMT. Sprintf ("Cannot sqrt negative number:%f", data)}func sqrt (f float64) (Float64, error) { If f < 0 { var data my float data = myfloat (f) return 0, data } return 0, Nil}func main () { fmt. Println (SQRT (2)) FMT. Println (Sqrt (-2))}
reference: http://go-tour-zh.appspot.com/#54