Skip to Content
Course content

41: Formatting and Linting with gofmt and vet

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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:

  1. Create a file named report.go with 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")
    }
    
  2. Run gofmt -w report.go to fix the indentation and structural alignment.
  3. Run go vet report.go. Identify the two issues it flags (the incorrect Printf verb and the unreachable code).
  4. Fix the bugs in the code so that go vet reports no issues.
  5. Run the program to ensure the output is now correct: User: Bob, Age: 30.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.