Static Analysis with go vet
Start with Static Analysis with go vet
In this lesson, explore static analysis with go vet through one small Go program, and connect its output to the language rules behind it.
What you’ll learn
- Explain the main purpose of static analysis with go vet.
- Read and run the complete Go example in this lesson.
- Apply the concept and recognize common mistakes.
Understand the idea
go vet examines code for patterns likely to be mistakes, such as suspicious formatting arguments and misuse of some APIs. Run go vet ./... over a module.
Vet is not a compiler, unit test or complete proof of correctness. Combine it with the race detector, tests and review.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Tools, syntax and practical notes
Command: go vet ./.... Official reference: cmd/vet.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import "fmt"
func main() {
name := "Go"
version := 1
fmt.Printf("%s lesson %d\n", name, version)
}
Output
Go lesson 1Trace what the program does
Printf receives a string for %s and an integer for %d. This is valid code that should pass vet’s format checks.
Avoid common mistakes
- Format strings and argument types should match.
- A clean vet run does not guarantee absence of all bugs.
Try it yourself
Try a deliberately mismatched fmt.Printf argument in a scratch file and see how go vet reports it.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Static Analysis with go vet in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.