加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Golang中获取包下的所有结构?

发布时间:2020-12-16 09:29:08 所属栏目:大数据 来源:网络整理
导读:我们可以在包下面以名称或接口的形式列出所有结构吗? 喜欢: struct := list("fmt") 预期结果: FormatterGoStringerScannerStateStringer 解决方法 您可以做的最好的事情是解析 go sources( you can clone:hg clone https://code.google.com/p/go/),并隔
我们可以在包下面以名称或接口的形式列出所有结构吗?
喜欢:

struct := list("fmt")

预期结果:

Formatter
GoStringer
Scanner
State
Stringer

解决方法

您可以做的最好的事情是解析 go sources( you can clone:hg clone https://code.google.com/p/go/),并隔离 ast.StructType.

这就是pretty printer does:

func (P *Printer) Type(t *AST.Type) int {
    separator := semicolon;

    switch t.form {

    case AST.STRUCT,AST.INTERFACE:
            switch t.form {
            case AST.STRUCT: P.String(t.pos,"struct");
            case AST.INTERFACE: P.String(t.pos,"interface");
            }
            if t.list != nil {
                    P.separator = blank;
                    P.Fields(t.list,t.end);
            }
            separator = none;

同样的想法,linter go/lint做同样的in lint.go:

case *ast.StructType:
        for _,f := range v.Fields.List {
            for _,id := range f.Names {
                check(id,"struct field")
            }
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读