// 定义错误信息映射 var ErrorMessage = map[int]string{ ErrorInvalidInput: "Invalid input, please check your input parameters", ErrorUnauthorized: "Unauthorized, please login first", ErrorNotFound: "The resource you requested could not be found", ErrorInternalServer: "Internal server error, please try again later", ErrorConnectionRefused: "Connection refused, please try again later", }
// 定义错误码类型 type ErrorCode int
// 实现错误码的字符串化方法 func(e ErrorCode) String() string { if msg, ok := ErrorMessage[int(e)]; ok { return fmt.Sprintf("%d: %s", e, msg) } return fmt.Sprintf("%d: Unknown error", e) }
// 定义错误信息映射 var ErrorMessage = map[int]string{ ErrorInvalidInput: "Invalid input, please check your input parameters", ErrorUnauthorized: "Unauthorized, please login first", ErrorNotFound: "The resource you requested could not be found", ErrorInternalServer: "Internal server error, please try again later", ErrorConnectionRefused: "Connection refused, please try again later", }
// 定义错误码类型 type ErrorCode int
// 实现错误码的字符串化方法 func(e ErrorCode) String() string { if msg, ok := ErrorMessage[int(e)]; ok { return fmt.Sprintf("%d: %s", e, msg) } return fmt.Sprintf("%d: Unknown error", e) }
// 定义错误信息映射 var ErrorMessage = map[int]string{ ErrorInvalidInput: "Invalid input, please check your input parameters", ErrorUnauthorized: "Unauthorized, please login first", ErrorNotFound: "The resource you requested could not be found", ErrorInternalServer: "Internal server error, please try again later", ErrorDatabase: "Database error, please try again later", }
errors.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package errors
import"fmt"
// 定义错误码类型 type ErrorCode int
// 实现错误码的字符串化方法 func(e ErrorCode) String() string { if msg, ok := ErrorMessage[int(e)]; ok { return fmt.Sprintf("%d: %s", e, msg) } return fmt.Sprintf("%d: Unknown error", e) }
// 定义错误信息映射 var ErrorMessage = map[int]string{ ErrorInvalidInput: "Invalid input, please check your input parameters", ErrorUnauthorized: "Unauthorized, please login first", ErrorNotFound: "The resource you requested could not be found", ErrorInternalServer: "Internal server error, please try again later", ErrorDatabase: "Database error, please try again later", }
errors.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package errors
import"fmt"
// 定义错误码类型 type ErrorCode int
// 实现错误码的字符串化方法 func(e ErrorCode) String() string { if msg, ok := ErrorMessage[int(e)]; ok { return fmt.Sprintf("%d: %s", e, msg) } return fmt.Sprintf("%d: Unknown error", e) }
// 定义错误信息映射 var ErrorMessage = map[int]string{ ErrorInvalidInput: "Invalid input, please check your input parameters", ErrorUnauthorized: "Unauthorized, please login first", ErrorNotFound: "The resource you requested could not be found", ErrorInternalServer: "Internal server error, please try again later", ErrorDatabase: "Database error, please try again later", }
types.go
1 2 3 4 5
goCopy code package errors
// 定义错误码类型 type ErrorCode int
format.go
1 2 3 4 5 6 7 8 9 10 11 12
goCopy code package errors
import"fmt"
// 实现错误码的字符串化方法 func(e ErrorCode) String() string { if msg, ok := ErrorMessage[int(e)]; ok { return fmt.Sprintf("%d: %s", e, msg) } return fmt.Sprintf("%d: Unknown error", e) }
在第二个例子中,我们使用if语句检查err变量的值是否等于errors.ErrorCode(errors.ErrorInvalidInput)。如果相等,则输出字符串"The error code is equal to ErrorInvalidInput"。这样,我们就可以方便地使用错误码常量来创建和比较错误变量了。