• go generate用法


    go generate用法

    1.generate命令

    $ go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
    
    //添加注释
    //go:generate command argument...   //注意:双斜线之后没有空格
    

    2.使用 go generate 工具编译 protobuf

    //go:generate protoc --go_out=. *.proto
    
    package test
     或者
    
    //go:generate sh -c "protoc --go_out=. *.proto"
    
    package test
    
    说明:这里stringer需要手动安装
     go get golang.org/x/tools/cmd/stringer
    
    或者:
    $ git clone https://github.com/golang/tools/ $GOPATH/src/golang.org/x/tools
    $ go install golang.org/x/tools/cmd/stringer
    

    3.自动生成 Stringer 接口

    package painkiller
    
    type Pill int
    
    const (
        Placebo Pill = iota
        Aspirin
        Ibuprofen
        Paracetamol
        Acetaminophen = Paracetamol
    )
    
    func (p Pill) String() string {
        switch p {
        case Placebo:
            return "Placebo"
        case Aspirin:
            return "Aspirin"
        case Ibuprofen:
            return "Ibuprofen"
        case Paracetamol: // == Acetaminophen
            return "Paracetamol"
        }
        return fmt.Sprintf("Pill(%d)", p)
    }
    
    =============================================
    //go:generate stringer -type=Pill
    
    package painkiller
    
    type Pill int
    
    const (
        Placebo Pill = iota
        Aspirin
        Ibuprofen
        Paracetamol
        Acetaminophen = Paracetamol
    )
    
    ==============================================
    // 自动生成 pill_string.go
    
    // generated by stringer -type Pill pill.go; DO NOT EDIT
    
    package painkiller
    
    import "fmt"
    
    const _Pill_name = "PlaceboAspirinIbuprofenParacetamol"
    
    var _Pill_index = [...]uint8{0, 7, 14, 23, 34}
    
    func (i Pill) String() string {
        if i < 0 || i+1 >= Pill(len(_Pill_index)) {
            return fmt.Sprintf("Pill(%d)", i)
        }
        return _Pill_name[_Pill_index[i]:_Pill_index[i+1]]
    }
    

    4.Error错误的处理

    (1.)传统的错误处理,通过map返回

    package errcode
    
    import "fmt"
    
    // 定义错误码
    const (
        ERR_CODE_OK = 0 // OK
        ERR_CODE_INVALID_PARAMS = 1 // 无效参数
        ERR_CODE_TIMEOUT = 2 // 超时
        // ...
    )
    
    // 定义错误码与描述信息的映射
    var mapErrDesc = map[int]string {
        ERR_CODE_OK: "OK",
        ERR_CODE_INVALID_PARAMS: "无效参数",
        ERR_CODE_TIMEOUT: "超时",
        // ...
    }
    
    // 根据错误码返回描述信息
    func GetDescription(errCode int) string {
        if desc, exist := mapErrDesc[errCode]; exist {
            return desc
        }
        
        return fmt.Sprintf("error code: %d", errCode)
    }
    

    (2.)使用string()方法实现

    // ErrCode used to define response code
    type ErrCode uint32
    
    //go:generate stringer -type ErrCode -output code_string.go
    或者//go:generate stringer -type ErrCode -linecomment -output code_string.go
    const (
        ERR_CODE_OK ErrCode = 0 // OK
        ERR_CODE_INVALID_PARAMS ErrCode = 1 // 无效参数
        ERR_CODE_TIMEOUT ErrCode = 2 // 超时
    )
    
    
    //注意:执行go generate 会在同一个目录下生成一个文件errcode_string.go
    func (i ErrCode) String() string {
       // xxx
    }
    
    //为了防止忘记手动generate,可以写到makefile中
    all:
        go generate && go build .
    
    go-swag:
    	swag init --parseVendor -g ./main.go
    

    5.其他用法

    package main
    
    import "fmt"
    
    //go:generate echo GoGoGo!
    //go:generate go run main.go
    //go:generate echo $GOARCH $GOOS $GOFILE $GOLINE $GOPACKAGE
    
    func main() {
     fmt.Println("go rum main.go!")
    }
    

    相关链接

    http://c.biancheng.net/view/4442.html
    https://www.xiaoheidiannao.com/70045.html
    https://juejin.cn/post/6844903923166216200
    https://islishude.github.io/blog/2019/01/23/golang/go-generate-预处理器教程/

    【励志篇】: 古之成大事掌大学问者,不惟有超世之才,亦必有坚韧不拔之志。
  • 相关阅读:
    Codeforces Round #359 (Div. 2) C. Robbers' watch 鸽巢+stl
    poj 3616 Milking Time dp
    UVA 11401
    UVA 11806 Cheerleaders dp+容斥
    Educational Codeforces Round 4 D. The Union of k-Segments 排序
    Codeforces Round #337 (Div. 2) C. Harmony Analysis 数学
    Codeforces Round #337 (Div. 2) B. Vika and Squares 水题
    Codeforces Round #337 (Div. 2) A. Pasha and Stick 水题
    POJ2402/UVA 12050 Palindrome Numbers 数学思维
    UVA 11076 Add Again 计算对答案的贡献+组合数学
  • 原文地址:https://www.cnblogs.com/tomtellyou/p/14665475.html
Copyright © 2020-2023  润新知