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
41: Formatting and Linting with gofmt and vet
A few years ago, I spent a grueling forty-minute code review arguing with a teammate about the alignment of struct tags and where exactly the closing brace of a long if block should sit. It was an exhausting, pedantic waste of time. My lead developer eventually stepped in, ran a single command in the terminal, and the entire file shifted into a standardized layout. He looked at me and said, "In Go, we don't argue about style. We just use the tool." That was the moment I realized that one of Go's greatest gifts isn't just its concurrency model, but its refusal to let developers bicker over whitespace.
The End of Style Debates
In most languages, you have to choose a style guide—Google's, Airbnb's, or some custom internal document—and then install a complex linter to enforce it. Go takes a different approach: gofmt. This tool doesn't just suggest a style; it defines the official style of the language. If your code isn't formatted with gofmt, it's simply considered "wrong" by the community.
When you run gofmt on a file, it handles the indentation (using tabs, not spaces), the spacing around operators, and the alignment of assignments. If you want to apply those changes directly to the file instead of just printing them to the console, you use the -w flag:
gofmt -w main.go
You'll likely find that your IDE is already doing this for you every time you hit save. I highly recommend keeping that setting enabled. It removes the cognitive load of formatting and lets you focus entirely on the logic. If you're working on a larger project, you can use go fmt ./... to recursively format every file in your current module.
Catching Bugs Before They Run
While gofmt makes your code look professional, go vet makes sure it actually makes sense. Think of it as a static analysis tool that looks for "suspicious" constructs that are syntactically correct (so the compiler is happy) but are almost certainly bugs.
I've seen go vet save me from countless production outages. For example, imagine you're using fmt.Printf and you accidentally use a %d (integer) verb for a string variable. The Go compiler won't always catch this because Printf takes an empty interface any. However, go vet will spot the mismatch immediately.
package main
import "fmt"
func main() {
name := "Alice"
// This compiles, but go vet will flag it as a mistake
fmt.Printf("Hello, %d", name)
}
If you run go vet main.go on the snippet above, it will warn you that the verb %d is not compatible with a string. It also catches other common slips, like unreachable code after a return statement or incorrectly formatted struct tags that would cause JSON unmarshaling to fail silently. It's an essential part of your pre-commit workflow; I never push code to a repository without running it first.
📋 Practical Task
Fixing the Legacy User Report
You've been handed a "legacy" file called report.go that was written by a developer who ignored all Go conventions. The code works, but it's a visual mess and contains a subtle bug that go vet can find.
Your Task:
- Create a file named
report.gowith the following content:package main import "fmt" type User struct { Name string `json:"name"` Age int `json:"age"` } func main() { u := User{Name: "Bob", Age: 30} fmt.Printf("User: %s, Age: %s\n", u.Name, u.Age) return fmt.Println("This should never run") } - Run
gofmt -w report.goto fix the indentation and structural alignment. - Run
go vet report.go. Identify the two issues it flags (the incorrect Printf verb and the unreachable code). - Fix the bugs in the code so that
go vetreports no issues. - Run the program to ensure the output is now correct:
User: Bob, Age: 30.
There are no comments for now.