3

I want to include some example codes in my comment for a function, like this:

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

But the code block is not displayed properly in vscode. What is the proper way to do it?

golopot
  • 10,726
  • 6
  • 37
  • 51

1 Answers1

10

Remove those backticks and just indent the code:

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

Quoting from 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.

Related questions:

Godoc documentation not outputting lists

GoDoc add newline character

What are Go example functions?

icza
  • 389,944
  • 63
  • 907
  • 827