【问题标题】:Godoc documentation not outputting listsGodoc 文档不输出列表
【发布时间】:2019-03-15 14:46:35
【问题描述】:

在我负责测试和记录的整个项目中,我为函数和方法创建了文档,格式如下:

// CheckPermissionArray checks that values is an array that contains the `expectedValue`
//
// Parameters:
//
// - `values` : the array to check
// - `expectedValue` : the value to search for
//
// Returns:
//
// - an error iff `expectedValue` is not in `values`

老板和其他程序员认可这种格式,但问题是godoc不识别列表:

有没有办法让列表被识别?

在某种程度上,Visual Studio Code 可以很好地识别此文档,尽管有点错误。

【问题讨论】:

标签: go godoc


【解决方案1】:

正如其他人所指出的,cmets 中的“列表”不会转换为 HTML 列表(例如 <ol><ul>)。

推荐阅读:Godoc: documenting Go code。引用自它:

Godoc 在概念上与 Python 的 Docstring 和 Java 的 Javadoc 相关,但它的设计更简单。 godoc 读取的 cmets 不是语言结构(与 Docstring 一样),它们也不一定有自己的机器可读语法(与 Javadoc 一样)。 Godoc cmets 只是很好的 cmets,即使 godoc 不存在,您也会想阅读的那种。

生成 HTML 文档时仅执行以下转换:

Godoc 在将 cmets 转换为 HTML 时使用了一些格式规则:

  • 随后的文本行被视为同一段落的一部分;您必须在段落之间留一个空行。
  • 预格式化文本必须相对于周围的评论文本缩进(参见 gob 的 doc.go 示例)。
  • URL 将被转换为 HTML 链接;不需要特殊标记。

你可以做什么来“模仿”列表:

使用以下注释:

// Fv1 is just an example.
//
// Here's a list:
//
// -First item
//
// -Second item
//
// -Third item
//
// This is the closing line.

结果如下:

Fv1 只是一个例子。

这是一个列表:

-第一项

-第二项

-第三项

这是结束语。

提供更好视觉外观的细微变化是使用项目符号 • 字符而不是破折号:

// Fv1 is just an example.
//
// Here's a list:
//
// • First item
//
// • Second item
//
// • Third item
//
// This is the closing line.

导致(github.com/google/go-cmp 使用它):

Fv1 只是一个例子。

这是一个列表:

• 第一项

• 第二项

• 第三项

这是结束语。

或者您可以缩进列表项(1 个额外的空间就足够了,但您可以根据自己的喜好使用更多空间):

// Fv2 is just another example.
//
// Here's a list:
//  -First item
//  -Second item
//  -Third item
//
// This is the closing line.

在生成的文档中产生这个:

Fv2 只是另一个例子。

这是一个列表:

-First item
-Second item
-Third item

这是结束语。

您可以像这样创建“嵌套”列表,保留标识(因为它将是一个预先格式化的块):

// Fv3 is just another example.
//
// Here's a list:
//   -First item
//     -First.First item
//     -First.Second item
//   -Second item
//
// This is the closing line.

文档中的结果:

Fv3 只是另一个例子。

这是一个列表:

-First item
  -First.First item
  -First.Second item
-Second item

这是结束语。

【讨论】:

  • @Adrian 发表评论几分钟后,我偶然发现了这个事实,但感谢您提供更多信息和强化。
【解决方案2】:

这可能会在未来通过“https://github.com/golang/go/issues/51082”(2022 年 2 月)改变

此提议改进了对 Go doc cmets 中的标题、列表和链接的支持,同时保持与现有 cmets 的向后兼容。

它包括一个新包go/doc/comment,它公开了一个已解析的语法树 doc cmets,它包括对go/printer 和因此gofmt 的更改 以标准方式格式化 doc cmets。

例如,现有列表从左侧显示重新格式化为右侧显示:

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 2019-04-10
    • 2019-04-07
    • 2012-11-11
    • 2019-12-25
    • 2021-07-15
    • 1970-01-01
    • 2021-09-19
    • 2019-07-22
    相关资源
    最近更新 更多