syscallErrnoErrorinttype ErrCode intconstconst (
    ERR_INVALID_PARAM ErrCode = 1 // 非法参数
    ERR_INVALID_COOKIE ErrCode = 2 // 无效cookie
    // ...
)go generatestringer//go:generate stringer -type=ErrCode --linecommenterrcode_string.go// Code generated by "stringer -type=ErrCode --linecomment"; DO NOT EDIT.
package code
import "strconv"
const _ErrCode_name = "无效参数无效cookie"
var _ErrCode_index = [...]uint8{0, 12, 24}
func (i ErrCode) String() string {
    i -= 1
    if i < 0 || i >= ErrCode(len(_ErrCode_index)-1) {
        return "ErrCode(" + strconv.FormatInt(int64(i+1), 10) + ")"
    }
    return _ErrCode_name[_ErrCode_index[i]:_ErrCode_index[i+1]]
}ErrCodeerrorfunc (e ErrCode) Error() string {
    return e.String()
}ErrCodeerrorgo generate可以放在项目打包脚本中执行。
测试:
package main
import (
    "fmt"
    "github.com/darjun/code" // 这是我的包,替换一下
)
func main() {
    var a error = code.ERR_INVALID_PARAM
    fmt.Println(a)
    // 输出:无效参数
    var b error = code.ERR_INVALID_COOKIE
    fmt.Println(b)
    // 输出:无效cookie
}