dongming5444 2016-05-27 16:38
浏览 958
已采纳

如何获得go语言功能的注释?

How to get annotation of go language function? Example:

// @annotation1
// @annotation2
func Tags() string {
        return ""
}

How to get the "@annotation1" and "@annotation2"?

  • 写回答

2条回答 默认 最新

  • dourao1968 2016-05-27 16:44
    关注

    Short answer, there is no native support for annotations in Golang. What's being used if tags, which you can get from the reflect package.

    So, you do not have annotations in Go, and to my knowledge there is no library which provides them. Depending of what you want to do, usually tags are more than enough, and you can use the language's power to achieve the desired results.

    It should be possible to implement them as you can get the documentation strings, just like PHP does. However, in the big majority of cases it won't be necessary.

    EDIT:

    In Go, you have access to the documentation of structs, fields, methods, interfaces, functions (godoc isn't magical) through the ast package. However, it requires parsing the files, there is no function such as type.getDocComments() as in PHP.

    So, an implementation is theoretically possible. However, the kind of annotations you're asking for are simply not part of Golang's philosophy. There are plenty of libraries that extensively use tags, but none use annotations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?