【问题标题】:Why go/doc doesn't provide the methods?为什么 go/doc 不提供方法?
【发布时间】:2015-05-08 06:31:29
【问题描述】:

我正在尝试使用 go/doc 包提取各种函数和方法的文档。代码如下所示。问题是它只打印/提供包中的功能(导出和未导出),但没有方法,无论它们是导出还是未导出。我也尝试了AllMethods 模式,但没有成功。这是一个错误还是我遗漏了什么?

pkg := doc.New(mainPkg, "./", doc.AllDecls)

for _, vv := range pkg.Funcs {
    log.Infof("vv.Name %v, vv.Recv %v", vv.Name,  vv.Recv,  )

}

【问题讨论】:

  • 您需要从迭代类型中提取方法,而不是从函数中提取。
  • 是的...我刚刚开始研究这个...
  • 当你弄明白的时候,也许会发布做它的方法作为答案
  • Types 字段中提供了这些方法(这对我来说似乎不正确),所以我不确定它是否值得回答。
  • 对我来说似乎完全正确 - 方法属于类型,而不是包本身。但如果它让你感到困惑,它可能会让其他人感到困惑,所以一个答案会很好。

标签: go


【解决方案1】:

遍历pkg.Types 并在每个项目上获得其Methods

type Type struct {
  Doc  string
  Name string
  Decl *ast.GenDecl

  // associated declarations
  Consts  []*Value // sorted list of constants of (mostly) this type
  Vars    []*Value // sorted list of variables of (mostly) this type
  Funcs   []*Func  // sorted list of functions returning this type
  Methods []*Func  // sorted list of methods (including embedded ones) of this type
}

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 2012-04-23
    • 1970-01-01
    • 2017-10-09
    • 2014-05-31
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多