在go函数注释中编写代码块的正确方法是什么?

问题描述

我想在函数的注释中包括一些示例代码,如下所示:

// Some example for using `foo`:
//
// ```
//   f := Foo(...)
//   g := Goo(f)
// ```
func Foo() {
  ...
}

但是代码块在vscode中无法正确显示。正确的方法是什么?

解决方法

删除这些反引号,然后缩进代码:

// Foo does ... (note this first line)
// Some example for using Foo:
//
//   f := Foo(...)
//   g := Goo(f)
func Foo() {
  ...
}

引用The Go Blog: Godoc: documenting Go code:

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

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

相关问题:

Godoc documentation not outputting lists

GoDoc add newline character

What are Go example functions?