程序员工具箱

GOLANG ERRORS 学习

2023-05-21  本文已影响0人  许一沐
package main

import "fmt"
// import "errors"

// type CustomError struct {
//  Code        int    `json:"code"`    // 业务码
//  Text        string `json:"message"` // 描述信息
// }

// func (e *CustomError) Error() string {
//  return e.Text
// }

var CustomErrors = map[int] string { 50: "算得上是", 10: "上是", 20: "算得是"}

type err int

func (e err) Error() string {
    return CustomErrors[int(e)]
}

func (e err) Code() int {
    return int(e)
}

func main() {

    rc := err(50)

    fmt.Println(errors.As(rc, error) )

    rr := err(10)

    fmt.Println(rr.Code(), rr.Error())

}

https://juejin.cn/post/7203341999956164664
https://juejin.cn/post/6920876156598091790
https://juejin.cn/post/6943786878780833806
https://zhuanlan.zhihu.com/p/84599497
https://zhuanlan.zhihu.com/p/380959953
https://juejin.cn/post/7027011560040038413

https://www.runoob.com/w3cnote/weui-for-weixin-web.html

上一篇 下一篇

猜你喜欢

热点阅读