首页 > 解决方案 > GoDoc 添加换行符

问题描述

我知道 Golang 支持通过以函数名称(拼写为“func”)开头的单行注释来记录函数。然而,有一个令人作呕的副作用:拥有多个单行注释不会产生一个 GoDoc 用换行符分隔每行文本

这是一张图片来说明:

在此处输入图像描述

这是函数及其文档:

//GetFunctionName gets function name
// Parameters:
// - `i` : Function
// **NOTE** this func fails if `i` is a variable set to a func
// (they're called "anonymous functions" in JavaScript)
func GetFunctionName(i interface{}) string {
    return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

如何在生成的文档中插入换行符?(如果这是 Javadoc,我会喜欢<br>,一切都会好起来的)

标签: godocumentationgodoc

解决方案


插入一个空的注释行,它将是一个新段落,这意味着它将从一个新行开始:

// GetFunctionName gets function name
//
// Parameters:
//   - `i` : Function
//
// **NOTE** this func fails if `i` is a variable set to a func
// (they're called "anonymous functions" in JavaScript)
func GetFunctionName(i interface{}) string {
    return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

推荐博文:Godoc:记录 Go 代码

相关部分:

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

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

推荐阅读