In Go, documentation explains how code behaves and how to use it, while a specification states precisely what the code must do or what the language guarantees. Documentation is descriptive and written for readers, mostly as doc comments and examples. A specification is normative: it defines requirements that an implementation, a test or a reviewer can check.
The word "specification" has two meanings in Go projects. One is the Go Language Specification, the official definition of the language. The other is the requirements and interface contracts for your own program. This article covers both and shows where doc comments, tests, interfaces and API schemas fit.
The easiest way to tell them apart is to ask what happens when they disagree with the code. If documentation is wrong, the documentation is a bug and gets fixed. If code violates a specification, the code is the bug.
| Aspect | Documentation | Specification |
|---|---|---|
| Purpose | Help someone understand and use the code | Define required behaviour and constraints |
| Tone | Explanatory, with examples | Precise and normative |
| Audience | Users and maintainers | Implementers, reviewers and testers |
| Checked by | Reading, go vet, tested examples | Tests, type checker, schema validation, review |
| Changes when | The code or its usage changes | The requirements change |
| Go examples | Doc comments, go doc, README.md | Language spec, interfaces, tests, OpenAPI or protobuf schemas |
Documentation lives in doc comments directly above declarations. The comment starts with the name it documents, and go doc and pkg.go.dev render it.
This comment tells a reader what Add does and mentions a behaviour worth knowing. It does not force the code to behave that way. Inline comments such as // retry once, the server closes idle connections explain why code exists and are not part of the public documentation.
Example functions bring documentation closer to a specification, because go test runs them and compares the output with the // Output: comment:
The official specification at go.dev/ref/spec is the formal definition of the language: syntax, types, expressions, statements, the memory model reference and the meaning of every built-in. Compiler authors, tool authors and anyone settling an argument about what is legal Go consult it. Each Go release can extend it, such as generics in Go 1.18, range over integers in Go 1.22 and range over functions in Go 1.23.
Unlike a tutorial, the spec does not teach. It states rules, for example that array length is part of the array type, so [3]int and [4]int are different types. That is why tools such as gofmt, go vet and gopls and alternative compilers all agree on what a valid program is.
For application code, a specification is any precise, checkable statement of what the program must do. Go gives you several ways to make those statements executable.
A type definition fixes the shape of data and the compiler enforces it.
An interface specifies the behaviour a caller depends on, and any type with the right methods satisfies it:
If an implementation lacks a method, the build fails. This is a specification enforced by the compiler.
Requirements such as "an empty name is rejected" or "the handler responds within 200 ms" become table-driven tests:
Performance requirements are expressed with benchmarks (func BenchmarkXxx(b *testing.B)) and compared across commits, not with comments alone.
For services, the specification is often a schema outside the Go code: an OpenAPI document for HTTP APIs or a .proto file for gRPC. Code generators produce Go types and server stubs from them, so the implementation cannot drift from the contract. Design documents and proposals capture requirements that are too large for one file.
Use both, for different jobs.
No. The specification at go.dev/ref/spec defines the language formally. The documentation, including the tour, effective Go guidance and package docs, explains how to use it.
Only loosely. A doc comment states intended behaviour, but nothing checks it unless you add example functions or tests that verify the same claim.
Put them in the form that can be checked: types and interfaces for structure, tests for behaviour and an OpenAPI or protobuf schema for network contracts. Explain the reasoning in a design document.
No. Where the spec says an order or result is unspecified, such as map iteration order, you must not depend on what the current implementation happens to do.
Documentation tells readers what the code does and how to use it, and it is allowed to be informal. A specification defines what must be true, and it is enforced by the compiler, tests or schemas. In Go, use doc comments and examples for the first, and types, interfaces, tests and external schemas for the second.