package main
import (
"fmt"
"strconv"
)
//ErrNegativeSqrt is alias of float64
type ErrNegativeSqrt float64
//String return the value of string
func (e ErrNegativeSqrt) String() string {
return strconv.FormatFloat(float64(e), 'g', 10, 64) + " is the result"
}
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprint(float64(e), " occurs the error")
}
func main() {
var a ErrNegativeSqrt = 2
fmt.Println(a)
}
为什么这段代码会输出 Error() 方法的结果?当把Error()方法删除后,就会输出String()方法的结果?
摘抄一段print.go中的代码,也许是你想要的答案?