V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
japeth
V2EX  ›  Go 编程语言

golang ast 生成函数时,函数的注释跟函数关联不起来(生成的位置有误),有大佬帮忙解答一下吗?

  •  
  •   japeth · 250 天前 · 940 次点击
    这是一个创建于 250 天前的主题,其中的信息可能已经有所发展或是发生改变。

    stackoverflow 地址: https://stackoverflow.com/questions/76947041/code-generation-in-golang-using-the-go-ast-package-to-add-doc-comments-but-comm

    当我在 errrmsg 目录中执行 GO GENERATE 时,函数的注释不在正确的位置

    // Code generated by gen_code. DO NOT EDIT.
    // 提示内容:"成功"
    // 状态码:10000
    // 提示内容:"失败"
    // 状态码:10001
    
    package errmsg
    
    const (
        TypeCodeSuccess = 10000
        TypeCodeFailed  = 10001
    )
    
    func GetSuccess() Resp {
        return Resp{Code: TypeCodeSuccess, Msg: TypeMsgSuccess}
    }
    
    func GetFailed() Resp {
        return Resp{Code: TypeCodeFailed, Msg: TypeMsgFailed}
    }
    

    这是我的代码地址: https://github.com/KingPuiWong/generrcode/blob/master/gencode/main.go

    当我试着这样做,但还是不起作用时,你知道怎么解决它吗?谢谢。

        commentMap := ast.NewCommentMap(fset, file, file.Comments)
        ast.Inspect(file, func(node ast.Node) bool {
            switch x := node.(type) {
            case *ast.FuncDecl:
                if strings.HasPrefix(x.Name.Name, "Get") {
                    commentText := x.Doc.List[0].Text
                    fmt.Println()
                    fmt.Printf("comment:%s", commentText)
                    commentMap[x] = []*ast.CommentGroup{{List: []*ast.Comment{{Text: commentText, Slash: token.Pos(int(x.Pos() - 1))}}}}
                }
            }
            return true
        })
    
        ast.Print(fset, file)
        //os.Exit(1)
        // 保存已分配的错误码
        err = saveLastErrorCode(projectName, lastCode)
        if err != nil {
            return err
            }
    

    这是我想要生成的。

    // Code generated by gen_code. DO NOT EDIT.
    package errmsg
    
    const (
        TypeCodeSuccess = 10000
        TypeCodeFailed  = 10001
    )
    
    // 提示内容:"成功"
    // 状态码:10000
    func GetSuccess() Resp {
        return Resp{Code: TypeCodeSuccess, Msg: TypeMsgSuccess}
    }
    
    
    // 提示内容:"失败"
    // 状态码:10001
    func GetFailed() Resp {
        return Resp{Code: TypeCodeFailed, Msg: TypeMsgFailed}
    }
    
    9 条回复    2023-08-23 14:27:52 +08:00
    8520ccc
        1
    8520ccc  
       249 天前 via iPhone
    代码生成推荐用 text/template ,舒服多了
    joesonw
        2
    joesonw  
       249 天前 via iPhone
    ast 包一般都拿来读的,第一次见到写的。
    代码生成用 text/template 或者 jennifer 。
    chai2010
        3
    chai2010  
       249 天前
    感觉 pos 位置计算有点问题,int(x.Pos() - 1) 可能不是前一行。

    pos 的定义可参考这里 https://chai2010.cn/go-ast-book/ch1/index.html 的 1.5 Position 位置信息
    joyme
        4
    joyme  
       249 天前
    https://go.dev/talks/2015/gofmt-en.slide#14 gofmt 的作者介绍过 comments 的处理,不知道对你是否有用。
    japeth
        5
    japeth  
    OP
       249 天前
    @8520ccc
    @joesonw
    谢谢,想先学习一下如何用 ast 直接生成,然后再写一个用 text/template 生成的
    japeth
        6
    japeth  
    OP
       249 天前
    @chai2010 晚上回去试一下,但是我猜测大概率不会是这个问题
    japeth
        7
    japeth  
    OP
       249 天前
    @chai2010 改成 x.post() 也是一样的效果
    harmless
        8
    harmless  
       248 天前 via iPhone
    可以试试 github.com/dave/dst ,注释友好,用法与 ast 差不多
    japeth
        9
    japeth  
    OP
       248 天前
    @harmless 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1176 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:57 · PVG 01:57 · LAX 10:57 · JFK 13:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.