liveBook · Manning

Static analysis

As explained in Shipping Go: Develop, deliver, discuss, design, and go again

Static code analysis is the automated examination of source code without executing it to identify defects, suspicious constructs, style violations, or security risks.

A code review is more useful when reviewers do not have to catch every routine mistake. Static analysis checks source code without running the application, looking for known patterns associated with bugs, security vulnerabilities, or poor practices. It does not replace human review; it gives reviewers more time for work that needs judgment. section 5.4

Start with a fast check. In a Go repository, `go vet ./...` checks all packages and normally produces no output when it finds no issue. For example, adding an extra argument to a `fmt.Sprintf` call gives `go vet` a format mismatch to report before that change reaches testing. section 5.4

Make the checks part of the normal feedback path:

  • Run formatting checks first, then `go vet`, then tests. Put the faster checks earlier so developers get specific feedback without waiting for later stages. section 5.4 section 5.8
  • Use `golangci-lint` for additional checks. A `.golangci-lint.yml` configuration can enable `gosec` for security-related findings, while a CI lint job can run through the `golangci-lint` GitHub Action. section 5.4
  • Extend the same mechanism to documentation quality: `stylecheck`, `godot`, and `misspell` can check exported-code comments, punctuation, and spelling. Run `make static-check`, fix the findings, and push the change. section 5.5
Listing 5.11 \golangci-lint.yml
linters:
  enable:
    - gosec
    - godot                       #1
    - misspell
    - stylecheck
 
linters-settings:                 #2
  stylecheck:
    # Select the Go version to target. The default is '1.13'.
    go: "1.18"
    checks: ["all","ST1*"]
 
issues:
  exclude-use-default: false      #3
 
output:
  format: colored-line-number
 
  # print lines of code with issue, default is true
  print-issued-lines: false
 
  # print linter name in the end of issue text, default is true
  print-linter-name: true

The linter configuration shows how security checks and comment-quality checks can share one repeatable static-analysis command. section 5.5 section 5.4

Treat a finding as actionable feedback, not as a substitute for testing or review. Keep the checks in the pipeline so known anti-patterns are caught before deployment, and adjust the process as the team learns where feedback is most useful. section 5.4 section 5.8

How Shipping Go: Develop, deliver, discuss, design, and go again covers it

How deep it goes
Works through Go vet and configurable linter checks, including CI placement and local commands, without detailing the analyzers’ internals.
Examples
Uses a `fmt.Sprintf` argument mismatch, `go vet ./...`, and YAML configurations for `gosec` and comment-checking linters.
What it assumes
Assumes familiarity with Go source and a repository pipeline; builds on existing formatting and testing checks rather than teaching programming from zero.
What you can do after
Operate/configure it with Go vet and golangci-lint locally and in CI to flag known issues before tests and deployment.
How it connects
Connects static analysis to code review, formatting, testing, continuous integration, security checks, and code documentation.

Also explained in

Compare how 6 books explain static analysis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Shipping Go: Develop, deliver, discuss, design, and go again. The section links point to the parts of the book it draws on.