实例:
package main import "fmt" type DevideError struct {
devidee int
devider int
} func (de *DevideError) Error() string {
strFormat := `
Cannot proceed, the divider is zero.
dividee: %d
divider:
`
return fmt.Sprintf(strFormat, de.devidee)
} func Devide(varDevidee int, varDevider int) (result int, errorMsg string) {
if varDevider == {
dData := DevideError {
devidee: varDevidee,
devider: varDevider,
}
errorMsg = dData.Error()
return
} else {
return varDevidee / varDevider, ""
}
} func main() {
if result, errorMsg := Devide(, ); errorMsg == "" {
fmt.Println("100/10 = ", result)
} if _, errorMsg := Devide(, ); errorMsg != "" {
fmt.Println("errorMsg is :", errorMsg)
}
}
运行结果:
/ =
errorMsg is :
Cannot proceed, the divider is zero.
dividee:
divider: