jsrosetta

Standard library

Writing Documentation

Node.js's JSDoc compared to Go's doc comments, Rust's rustdoc, Swift's DocC, and Java's Javadoc, plus examples that run as tests.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.0Swift ≥ 2.0Java ≥ 23
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Node.js uses special comments following the JSDoc convention above a declaration, read by editors and doc generators through @... tags. Go doc comments are a toolchain convention, not part of the language spec: a // comment right above an exported declaration is automatically picked up by the go/doc package and rendered by go doc and pkg.go.dev, with no tag syntax needed at all. Rust uses /// comments (rustdoc) — close to Go, except the markdown inside (including code blocks) is rendered directly by cargo doc, and those code blocks are also actually run by cargo test as doc tests. Swift also uses /// with markdown, rendered into a documentation site by the DocC tool (since Swift 5.5). Java uses the traditional Javadoc /** */; since Java 23, /// (JEP 467) lets you write a doc comment in markdown instead of mixing HTML with tags.

Comments for a class/struct and its methods

/**
 * Creates a new Person.
 * @class
 * @example
 * const person = new Person('bob')
 */
class Person {
  /**
   * Create a person.
   * @param {string} [name] - The person's name.
   */
  constructor(name) {
    this.name = name
  }
 
  /**
   * Get the person's name.
   * @return {string} The person's name
   * @example
   * person.getName()
   */
  getName() {
    return this.name
  }
 
  /**
   * Set the person's name.
   * @param {string} name - The person's name.
   * @example
   * person.setName('bob')
   */
  setName(name) {
    this.name = name
  }
}

Run go doc Person right inside the package's directory to print its documentation on the command line; the same comments power the generated page on pkg.go.dev once the module is published, or locally via pkgsite (go run golang.org/x/pkgsite/cmd/pkgsite@latest) — the older standalone godoc server is deprecated in favor of pkgsite. Equivalents: cargo doc --open (Rust), swift package generate-documentation using the swift-docc-plugin (Swift), and the javadoc command or the corresponding Maven/Gradle plugin (Java).

Runnable documentation examples

package person
 
import "fmt"
 
// Example of creating a new Person.
func ExampleNewPerson() {
	person := NewPerson("bob")
	_ = person
}
 
// Example of getting person's name.
func ExamplePerson_GetName() {
	person := NewPerson("bob")
	fmt.Println(person.GetName())
	// Output: bob
}
 
// Example of setting person's name.
func ExamplePerson_SetName() {
	person := NewPerson("alice")
	person.SetName("bob")
	fmt.Println(person.GetName())
	// Output: bob
}
$ go test -v examples/documentation.go examples/documentation_test.go
=== RUN   ExamplePerson_GetName
--- PASS: ExamplePerson_GetName (0.00s)
=== RUN   ExamplePerson_SetName
--- PASS: ExamplePerson_SetName (0.00s)
PASS
ok  	command-line-arguments	0.620s

Rust goes further than Go: the markdown code block right inside a /// comment (as seen in the "Comments for a class/struct" section above) is the doc test — there's no separate Example-style function to write.

$ cargo test --doc
# the order of the two test lines can swap between runs
running 2 tests
test src/lib.rs - Person::set_name (line 30) ... ok
test src/lib.rs - Person::new (line 11) ... ok
 
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.74s

Reference: github.com/miguelmota/golang-for-nodejs-developers#documentation