go-tools

repository·master·Indexed 27 days ago

https://github.com/dominikh/go-tools

A collection of advanced Go development tools featuring staticcheck, a high-performance static analysis linter for detecting bugs and performance issues. The repository also includes structlayout for inspecting Go struct memory layout, structlayout-optimize for minimizing padding, and visualization tools like structlayout-pretty and structlayout-svg.

Tokens
10.6K
Snippets
22
Records
92
Agent score
75%

What's inside go-tools

  1. Overview of Go tools in this repository

    master

    The go-tools repository contains several command-line tools located in the cmd/ directory. Each tool serves a specific purpose for Go development:

    • staticcheck: Performs static analysis to detect bugs and performance issues.
    • structlayout: Displays the memory layout (field sizes and padding) of Go structs.
    • structlayout-optimize: Reorders struct fields to minimize padding.
    • structlayout-pretty: Formats structlayout output using ASCII art.
  2. Staticcheck 2023.1 (v0.4.0) Release Overview

    master
    Staticcheck 2023.1 adds support for Go 1.20 and introduces a complete rewrite of the U1000 check. The rewrite uses a more suitable program representation, resulting in fewer false positives and more true positives. This version also includes improvements to the intermediate representation, allowing various checks to identify more issues.
  3. Staticcheck 2020.2 Performance Improvements

    master

    Staticcheck 2020.2 introduced significant performance optimizations, reducing both memory usage and runtimes across various packages.

    Key changes include:

    • Substantial reductions in memory footprint (e.g., up to 70-80% reduction in some large packages like std or k8s.io/kubernetes/pkg/...).
    • Faster execution times for both cached and uncached runs.
    • Automatic skipping of large packages: Staticcheck will now skip packages that are 50 MiB or larger, assuming they contain bundled assets rather than source code, to further conserve memory.
  4. Install Staticcheck via go install

    master

    For Go 1.17 and later, the simplest way to install Staticcheck is using the go install command. This installs the binary to your $GOPATH/bin directory. You can install the latest version or specify a specific version using the @version syntax.

    To find your $GOPATH, run go env GOPATH.

    go install honnef.co/go/tools/cmd/staticcheck@latest
  5. Merge Staticcheck results from different build tags using `-merge`

    master

    To avoid false positives (like unused functions that are actually used in other build variants) and false negatives when code uses Go build tags, you can run Staticcheck multiple times with different configurations and merge the results.

    To use the -merge flag:

    1. Run Staticcheck for each configuration using the -f binary flag to output results in a binary format.
    2. Pass the resulting files as arguments to a final staticcheck -merge command, or pipe the outputs into staticcheck -merge via standard input.

    Note: When using -merge, arguments are interpreted as filenames containing binary output rather than import paths.

    # Method 1: Using files as arguments
    $ GOOS=linux staticcheck -f binary >file1
    $ GOOS=windows staticcheck -f binary >file2
    $ staticcheck -merge file1 file2
    
    # Method 2: Using a pipe (standard input)
    $ (
      GOOS=linux staticcheck -f binary
      GOOS=windows staticcheck -f binary
    ) | staticcheck -merge
  6. Automate multiple build configurations with the `-matrix` flag

    master

    The -matrix flag automates running Staticcheck across multiple build configurations and merging the results in a single step. This is ideal when all configurations can be checked on a single system (e.g., when not using cgo).

    Staticcheck reads a build matrix from standard input. Each non-empty line must follow this format: <name>: [environment variables] [flags]

    • <name>: A valid build name consisting of letters, numbers, and underscores.
    • [environment variables]: Go environment variables (e.g., GOOS=linux, GOARCH=amd64).
    • [flags]: Command-line flags passed to the go tool (e.g., -tags=debug).

    Results are annotated with the names of the build configurations in which the issues were found.

    $ staticcheck -matrix <<EOF
    windows: GOOS=windows
    linux: GOOS=linux
    appengine: GOOS=linux -tags=appengine
    EOF
  7. Run Staticcheck on your code

    master

    The staticcheck command follows the same package patterns as go build or go vet. You can run it on a single package or recursively on all packages in your module.

    • To check the current package: staticcheck .
    • To check all packages in the current directory and subdirectories: staticcheck ./...

    If no issues are found, the command will produce no output. If issues are detected, they will be printed to the console.

    staticcheck ./...
  8. Changes to Unused Code Detection (U1000)

    master

    The detection of unused code (the U1000 check) has undergone significant changes in version 2020.2:

    Removal of Whole-Program Mode

    Whole-program mode has been removed from the standard staticcheck tool. This mode previously analyzed entire programs to report unused exported identifiers but was prone to flakiness due to caching behavior. If your workflow strictly requires whole-program analysis and you can tolerate potential bugs, you should use Staticcheck 2020.1.

    Handling of Exported Identifiers

    To improve correctness, all exported package-level identifiers are now considered used, even if they are declared in package main or in test files. This accounts for use cases like the plugin build mode where usage might be invisible to the static analyzer.

  9. Configure Staticcheck using configuration files

    master

    Staticcheck uses staticcheck.conf files to apply settings to all users of a project. These files are applied to subtrees of packages. When multiple configuration files apply to a package, they are merged, with settings in files deeper in the package tree overriding rules higher up the tree.

    Example hierarchy:

    • ./staticcheck.conf (applies to all packages)
    • ./net/staticcheck.conf (applies to ./net/...)
    • ./net/http/staticcheck.conf (applies to ./net/http/...)