我们可以在包下以名称或接口(interface)的形式列出所有结构吗?
喜欢:
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")
}
}
}
关于go - 如何在Golang中的包下获取所有结构?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24118011/