Go
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions and Methods
-
Section 4: Concurrency
-
Section 5: Packages and Tooling
-
Section 6: More Standard Library
-
Section 7: Building Services
-
Section 8: Advanced Go
-
Section 9: Go in the Cloud-Native Ecosystem
-
Section 10: Data Structures and Algorithms in Go
-
Section 11: Testing and Deployment
-
Section 12: Practical Projects
-
Section 13: More Standard Library Practice
-
Section 14: More Practice Projects
-
Section 15: Design Patterns in Go
-
Section 16: Interview Practice
-
Section 17: Package fmt In Depth
-
Section 18: Package strings and strconv
-
Section 19: Package os and io
-
Section 20: Package time
-
Section 21: Package sort and container
-
Section 22: Package encoding
-
Section 23: Package net/http In Depth
-
Section 24: Package context
-
Section 25: Package regexp and bytes
-
Section 26: Package errors In Depth
-
Section 27: Package crypto and hash
-
Section 28: Package flag and log
-
Section 29: Package sync In Depth
-
Section 30: More Practice Exercises
-
Section 31: Go Modules and Workspaces In Depth
-
Section 32: Generics Deep Dive (Go 1.18+)
-
Section 33: Testing Package In Depth
-
Section 34: More Interview and Whiteboard Practice
-
Section 35: Package math and unicode
-
Section 36: Package path and filepath
-
Section 37: Package database/sql
-
Section 38: Package text/template and html/template
-
Section 39: Package archive and compress
-
Section 40: Lower-Level net Package
-
Section 41: Package runtime and reflect
-
Section 42: Package embed and io/fs
-
Section 43: Go Assembly and CGO Basics
-
Section 44: Building CLIs and TUIs
-
Section 45: Go Performance Tuning
-
Section 46: More Real-World Projects
-
Section 47: Go in Production
-
Section 48: Go Design Patterns
-
Section 49: Go Interfaces Deep Dive
-
Section 50: Final Practice and Review
42: Documentation with godoc
When you're working on a small project by yourself, you can keep the entire API in your head. But the second you share your code with a teammate or open-source it, you're going to get a barrage of questions. That's where godoc comes in. In Go, documentation isn't a separate chore you do in a Word doc or a Wiki; it's baked directly into your source code.
How do I write comments that actually show up in the docs?
It's simpler than you'd think, but there is one strict rule: the comment must be placed immediately preceding the declaration, with no empty lines in between. If you put a blank line between your comment and your function, Go treats it as just a regular comment and ignores it for documentation purposes.
Also, the convention is to start the comment with the name of the thing you're documenting. It feels repetitive at first, but it makes the generated documentation read like a professional manual.
package inventory
// Item represents a product in the warehouse.
type Item struct {
SKU string
Quantity int
}
// AddStock increases the quantity of the item by the given amount.
// It returns an error if the amount is negative.
func (i *Item) AddStock(amount int) error {
if amount < 0 {
return fmt.Errorf("cannot add negative stock")
}
i.Quantity += amount
return nil
}
I've seen a lot of devs try to write "This function adds stock..."—don't do that. Start with "AddStock increases...". It's the Go way.
Where do I actually view these documents while I'm coding?
You have two main options. If you want a quick answer without leaving your terminal, use the go doc command. For example, go doc inventory.Item will dump the documentation for that struct right into your shell. It's fast, efficient, and exactly what I use 90% of the time.
But if you want the full "web browser" experience—the one that looks like the official Go packages site—you'll want to use pkgsite (which is the modern successor to the original godoc tool). You can install it and run it locally to see your own project rendered as a website. It's a great way to check if your formatting looks right before you push your code to a repository.
What's the deal with "Example" functions?
The best documentation isn't a description; it's a runnable example. Go has a brilliant feature where you can write "Example" functions in your _test.go files, and godoc will automatically pull them into the documentation page for that function.
The trick is the naming convention: the function must start with Example, followed by the name of the function it's demonstrating. Then, you add a special comment at the end starting with // Output:. This turns the example into a test that the Go toolchain actually verifies.
// In inventory_test.go
package inventory
import "fmt"
func ExampleItem_AddStock() {
item := Item{SKU: "GOPHER-PLUSH", Quantity: 10}
item.AddStock(5)
fmt.Println(item.Quantity)
// Output: 15
}
When you view your docs, this code block appears right under the AddStock method. It's a win-win: you're providing a tutorial for your users, and you're writing a regression test for your code at the same time.
📋 Practical Task
Documenting the Warehouse Shipping Package
You've been handed a "Shipping" package that is functionally complete but totally undocumented. Your task is to make it professional and "Go-idiomatic".
- Create a package named
shipping. - Implement a
Shipmentstruct with fields forTrackingIDandWeight. - Implement a method
CalculateCost(rate float64) float64that returns the weight multiplied by the rate. - Add proper doc comments to the struct and the method (remember the naming convention!).
- Create a
shipping_test.gofile and implement anExampleShipment_CalculateCostfunction that demonstrates a shipment weighing 10kg at a rate of 2.5, printing the result. Include the// Output:comment so it's valid.
To verify your work, run go doc -all in your terminal to ensure your comments are being picked up correctly.
There are no comments for now.