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

What is the proper way to write a code block in a go function comment?

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

// 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:

There are a few formatting rules that Godoc uses when converting comments to HTML:

  • Subsequent lines of text are considered part of the same paragraph; you must leave a blank line to separate paragraphs.
  • Pre-formatted text must be indented relative to the surrounding comment text (see gob's doc.go for an example).
  • URLs will be converted to HTML links; no special markup is necessary.

相关问题:

Godoc documentation not outputting lists