go的错误表示方法
转载自链接
error简单表示方法
package errors
// New returns an error that formats as the given text.
func New(text string) error {
return &errorString{text}
}
// errorString is a trivial implementation of error.
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
error的结构体表示方法
允许携带更多信息
package errors
import "fmt"
type AreaError struct {
Err string
Radius float64
}
func (e *AreaError) Error() string{
return fmt.Sprintf("radius %0.2f: %s",e.Radius,e.Err)
}
main函数
package main
import (
"error/error"
"fmt"
"math"
)
func circleArea0(radius float64) (float64, error) {
if radius < 0 {
return 0, errors.New("Area calculation failed, radius is less than zero")
}
return math.Pi * radius * radius, nil
}
func circleArea1(radius float64) (float64, error) {
if radius < 0 {
return 0, &errors.AreaError{Err: "radius is negative", Radius: radius}
}
return math.Pi * radius * radius, nil
}
func main() {
radius := -20.0
//area, err := circleArea0(radius)
//if err != nil {
// fmt.Println(err)
// return
//}
//基于错误,反馈更多信息radius
area, err := circleArea1(radius)
if err != nil{
if err,ok := err.(*errors.AreaError);ok{
fmt.Printf("Radius %0.2f is less than zero\n", err.Radius)
return
}
fmt.Println(err)
return
}
fmt.Printf("Area of circle %0.2f", area)
}