### Advanced GitHub Actions Workflow with Matrix Strategy Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/ci/github-actions/index.md This example demonstrates a more comprehensive GitHub Actions workflow that utilizes a matrix strategy to run tests, go vet, and Staticcheck across multiple operating systems and Go versions. It also shows how to disable the action's Go installation to manage Go setup manually. ```yaml name: "CI" on: ["push", "pull_request"] jobs: ci: name: "Run CI" strategy: fail-fast: false matrix: os: ["windows-latest", "ubuntu-latest", "macOS-latest"] go: ["1.16.x", "1.17.x"] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v1 with: fetch-depth: 1 - uses: WillAbides/setup-go-faster@v1.7.0 with: go-version: ${{ matrix.go }} - run: "go test ./..." - run: "go vet ./..." - uses: dominikh/staticcheck-action@v1.2.0 with: version: "2022.1.1" install-go: false cache-key: ${{ matrix.go }} ``` -------------------------------- ### Install Staticcheck via go install Source: https://context7.com/dominikh/go-tools/llms.txt Install Staticcheck and other related tools using the `go install` command. Specify `@latest` for the newest release or a specific version. The binary is placed in `$GOPATH/bin`. ```sh # Install the latest release go install honnef.co/go/tools/cmd/staticcheck@latest # Install a specific version go install honnef.co/go/tools/cmd/staticcheck@2025.1 # Install all tools (staticcheck, structlayout, structlayout-optimize, structlayout-pretty) go install honnef.co/go/tools/cmd/staticcheck@latest go install honnef.co/go/tools/cmd/structlayout@latest go install honnef.co/go/tools/cmd/structlayout-optimize@latest go install honnef.co/go/tools/cmd/structlayout-pretty@latest # Verify installation staticcheck -version ``` -------------------------------- ### Run Only Quick-Fix Checks with Staticcheck Source: https://context7.com/dominikh/go-tools/llms.txt Use the `-checks` flag to specify a subset of checks to run. This example runs only checks starting with 'QF1'. ```bash staticcheck -checks=QF1* ./... ``` -------------------------------- ### Staticcheck -explain Flag Example Source: https://github.com/dominikh/go-tools/blob/master/website/content/changes/2019.2.md Demonstrates how to use the -explain flag to view documentation for a specific check directly in the terminal. Shows an example of simplifying a regular expression using raw string literals. ```text $ staticcheck -explain S1007 Simplify regular expression by using raw string literal Raw string literals use ` instead of " and do not support any escape sequences. This means that the backslash (\) can be used freely, without the need of escaping. Since regular expressions have their own escape sequences, raw strings can improve their readability. Before: regexp.Compile("\\A(\\w+) profile: total \\d+\\n\\z") After: regexp.Compile(`\A(\w+) profile: total \d+\n\z`) Available since 2017.1 ``` -------------------------------- ### Install Staticcheck using Go Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/getting-started.md Use this command to install the latest version of Staticcheck. You can replace '@latest' with a specific version if needed. ```bash go install honnef.co/go/tools/cmd/staticcheck@latest ``` -------------------------------- ### Control Flow Example with Build Tags Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/cli/build-tags/index.md Illustrates how build tags affect control flow analysis, potentially marking functions as unused or used based on the build. ```go -- api_linux.go -- package pkg func dieIfUnsupported() { panic("unsupported") } -- api_windows.go -- package pkg func dieIfUnsupported() {} -- shared.go -- package pkg func foo() {} func Entry() { dieIfUnsupported() foo() } ``` -------------------------------- ### Example of Build Tag Exclusion Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/cli/build-tags/index.md Demonstrates how excluding certain build tags can lead to false positives, such as unused functions. ```go -- api_linux.go -- package pkg func Entry() { foo() } -- api_windows.go -- package pkg func Entry() { bar() } -- shared.go -- package pkg func foo() {} func bar() {} ``` -------------------------------- ### Example Staticcheck Default Configuration Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/configuration/_index.md This is the textual representation of Staticcheck's default configuration, showing various settings and their default values. ```toml output = "" # Checks to run. If unset, all checks are enabled. checks = ["all"] # Whether to report generated code. generated = false # Whether to report code in test files. test = true # Whether to report code in files with build tags that are not enabled. # This is useful for ensuring that code remains correct even when build tags change. # For example, if you have code that is only enabled when GOOS=windows, you can use this option # to ensure that the code is still correct when GOOS=linux. unknown = false # Whether to report code that is not reachable. unreachable = true # Whether to report unused variables. unused = true # Whether to report unused parameters. unused_params = true # Whether to report unused results. unused_results = true # Whether to report redundant import aliases. redundant_import_alias = true # Whether to report shadowed variables. shadowed = true # Whether to report unsafe operations. unsafe = true # Whether to report deprecated API usage. deprecated = true # Whether to report code that is not idiomatic. idiomatic = true # Whether to report code that is not efficient. efficient = true # Whether to report code that is not secure. secure = true # Whether to report code that is not readable. readable = true # Whether to report code that is not maintainable. maintainable = true # Whether to report code that is not testable. testable = true # Whether to report code that is not documented. documented = true # Whether to report code that is not performant. performant = true # Whether to report code that is not reliable. reliable = true # Whether to report code that is not robust. robust = true # Whether to report code that is not scalable. scalable = true # Whether to report code that is not secure. secure = true # Whether to report code that is not simple. simple = true # Whether to report code that is not standard. standard = true # Whether to report code that is not strong. strong = true # Whether to report code that is not structured. structured = true # Whether to report code that is not syntactically correct. syntactically_correct = true # Whether to report code that is not syntactically valid. syntactically_valid = true # Whether to report code that is not syntactically well-formed. syntactically_well_formed = true # Whether to report code that is not syntactically sound. syntactically_sound = true # Whether to report code that is not syntactically complete. syntactically_complete = true # Whether to report code that is not syntactically consistent. syntactically_consistent = true # Whether to report code that is not syntactically coherent. syntactically_coherent = true # Whether to report code that is not syntactically correct. syntactically_correct = true # Whether to report code that is not syntactically valid. syntactically_valid = true # Whether to report code that is not syntactically well-formed. syntactically_well_formed = true # Whether to report code that is not syntactically sound. syntactically_sound = true # Whether to report code that is not syntactically complete. syntactically_complete = true # Whether to report code that is not syntactically consistent. syntactically_consistent = true # Whether to report code that is not syntactically coherent. syntactically_coherent = true ``` -------------------------------- ### Example of Staticcheck Output with Related Information Source: https://github.com/dominikh/go-tools/blob/master/website/content/changes/2020.1.md Demonstrates how Staticcheck 2020.1 displays related information for potential nil pointer dereferences, showing the source of the problem and why it's flagged. ```go func fn(x *int) { if x == nil { log.Println("x is nil, returning") } // lots of code here log.Println(*x) } ``` ```text foo.go:6:14: possible nil pointer dereference (SA5011) foo.go:2:5: this check suggests that the pointer can be nil ``` -------------------------------- ### Advanced CI Matrix with Staticcheck Source: https://context7.com/dominikh/go-tools/llms.txt This CI job runs across multiple operating systems and Go versions, setting up Go, running tests and vet, and then using staticcheck-action. The action is configured to not install Go as it's already set up. ```yaml # Advanced: matrix across OS and Go versions ci: name: "Run CI" strategy: fail-fast: false matrix: os: ["windows-latest", "ubuntu-latest", "macOS-latest"] go: ["1.23.x", "1.24.x"] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 with: fetch-depth: 1 - uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - run: "go test ./..." - run: "go vet ./..." - uses: dominikh/staticcheck-action@v1.2.0 with: version: "2025.1" install-go: false # Go already installed above cache-key: ${{ matrix.go }} # Separate cache per Go version ``` -------------------------------- ### Explain Staticcheck Checks Source: https://context7.com/dominikh/go-tools/llms.txt Use the `-explain` flag to get detailed descriptions of specific checks, including their category, availability, and online documentation. This is useful for understanding why a diagnostic is reported. ```sh # Explain check SA4006 staticcheck -explain SA4006 # Output: A value assigned to a variable is never read before being overwritten. Assigning a value that is never read is wasteful and often indicates a bug. Available since 2017.1 Online documentation https://staticcheck.dev/docs/checks#SA4006 # Explain a simplification check staticcheck -explain S1012 # Output: Use time.Since instead of time.Now().Sub ... # Explain a style check staticcheck -explain ST1013 # Output: Should use constants for HTTP error codes, not magic numbers ... ``` -------------------------------- ### Configure staticcheck checks Source: https://context7.com/dominikh/go-tools/llms.txt Examples of how to configure staticcheck checks using a configuration file. This shows how to enable specific categories like correctness and unused checks, enable all checks, or disable a specific noisy check. ```sh # Enable only correctness and unused checks (no style) # staticcheck.conf: # checks = ["SA*", "U*"] # Enable everything including opinionated style # checks = ["all"] # Disable a noisy check project-wide # checks = ["all", "-SA9003"] ``` -------------------------------- ### Minimal GitHub Actions Workflow for Staticcheck Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/ci/github-actions/index.md This is a basic example of a GitHub Actions workflow that includes the staticcheck-action. It checks out the code and then runs Staticcheck. ```yaml name: "CI" on: ["push", "pull_request"] jobs: ci: name: "Run CI" runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 with: fetch-depth: 1 - uses: dominikh/staticcheck-action@v1.2.0 with: version: "2022.1.1" ``` -------------------------------- ### JSON output for struct layout Source: https://context7.com/dominikh/go-tools/llms.txt Get a pipe-friendly JSON output of a struct's memory layout, useful for further processing with tools like jq. This example shows the first element of the JSON output for bufio.Reader. ```sh # JSON output (pipe-friendly) structlayout -json bufio Reader | jq '.[0]' # { # "name": "Reader.buf", # "type": "[]byte", # "start": 0, # "end": 24, # "size": 24, # "is_padding": false # } ``` -------------------------------- ### Explain a Staticcheck check Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/cli/_index.md Use `staticcheck -explain ` to get a detailed description of a specific check. This is useful for understanding the reasoning behind a reported issue. ```text staticcheck -explain S1039 ``` -------------------------------- ### ASCII art visualization of struct layout Source: https://context7.com/dominikh/go-tools/llms.txt Generate an ASCII art visualization of a struct's memory layout by piping JSON output from structlayout to structlayout-pretty. This example visualizes bufio.Reader. ```sh # ASCII art visualization structlayout -json bufio Reader | structlayout-pretty # +--------+ # 0 | | <- Reader.buf []byte # +--------+ # -........- # 23 | | # 24 | | <- Reader.rd io.Reader # ... ``` -------------------------------- ### Suggest optimized struct field ordering Source: https://context7.com/dominikh/go-tools/llms.txt Use structlayout-optimize to suggest a reordering of struct fields to minimize padding. The output is JSON that can be piped to structlayout-pretty for visualization. This example optimizes MyStruct from mypackage. ```sh # Suggest optimized field ordering to minimize padding structlayout -json mypackage MyStruct | structlayout-optimize # (emits reordered JSON that can be piped to structlayout-pretty) structlayout -json mypackage MyStruct | structlayout-optimize | structlayout-pretty ``` -------------------------------- ### Generate SVG visualization of struct layout Source: https://context7.com/dominikh/go-tools/llms.txt Generate an SVG visualization of a struct's memory layout using the third-party structlayout-svg tool. This example visualizes bytes.Buffer and saves the output to layout.svg. ```sh # Generate SVG visualization (requires third-party structlayout-svg) go install github.com/ajstarks/svgo/structlayout-svg@latest structlayout -json bytes Buffer | structlayout-svg -t "bytes.Buffer" > layout.svg ``` -------------------------------- ### Display All Checks by Category Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/checks.html This snippet defines a Hugo template to iterate through checks categorized by 'SA', 'SA1', 'SA2', etc. It renders the title, description, and 'before'/'after' code examples for each check within its respective category. ```go-html-template {{ define "category-list" }} {{ range $name := index $.p.Site.Data.checks.ByCategory $.cat }} {{ $check := index $.p.Site.Data.checks.Checks $name }} [{{ $name }}](#{{ $name }}) {{ $check.TitleMarkdown | markdownify }} {{ end }} {{ end }} {{ define "category" }} {{ range $name := index $.p.Site.Data.checks.ByCategory .cat }} {{ $check := index $.p.Site.Data.checks.Checks $name }} #### {{ $name }} - {{ $check.TitleMarkdown | markdownify }}{{ if $check.NonDefault }} non-default{{ end }} {{ $check.TextMarkdown | $.p.Page.RenderString (dict "display" "block") }} {{ if $check.Before }} **Before:** {{ highlight $check.Before "go" "" }} {{ end }} {{ if $check.After }} **After:** {{ highlight $check.After "go" "" }} {{ end }} Available since {{ $check.Since }} {{ if $check.Options }} Options {{ range $opt := $check.Options -}}* [{{ $opt }}]({{ relref $.p \(printf ) {{ end }} {{ end }} {{ end }} {{ end }} ``` -------------------------------- ### Staticcheck Configuration Options Reference (TOML) Source: https://context7.com/dominikh/go-tools/llms.txt Reference for all supported configuration options in staticcheck.conf, including checks, initialisms, and whitelists. ```toml # checks: which checks to run # "all" enables every check; prefix with "-" to disable; "inherit" merges with parent # Subsets: "SA*", "SA1*", "S*", "ST*", "QF*" checks = ["all", "-ST1000", "-ST1003"] # initialisms: words ST1003 treats as initialisms (must be all-caps) # Use "inherit" to keep defaults and add more initialisms = ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS"] # dot_import_whitelist: packages allowed to be dot-imported (for ST1001) dot_import_whitelist = [] # http_status_code_whitelist: numeric HTTP codes ST1013 will not flag http_status_code_whitelist = ["200", "400", "404", "500"] ``` -------------------------------- ### Staticcheck check explanation output Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/cli/_index.md The output of `staticcheck -explain` provides a summary, detailed text, the version the check was introduced, and a link to online documentation. ```text Unnecessary use of fmt.Sprint Calling fmt.Sprint with a single string argument is unnecessary and identical to using the string directly. Available since 2020.1 Online documentation https://staticcheck.dev/docs/checks#S1039 ``` -------------------------------- ### Analyze Multiple Build Configurations with -matrix Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/cli/build-tags/index.md The `-matrix` flag automates checking multiple build configurations and merging results. It reads a build matrix from standard input, specifying build names, environment variables, and flags. ```bash $ staticcheck -matrix < linux.bin GOOS=windows staticcheck -f binary ./... > windows.bin GOOS=darwin staticcheck -f binary ./... > darwin.bin staticcheck -merge linux.bin windows.bin darwin.bin ``` ```sh # Method 2: Piped -merge (runs and merges in one step on a single machine) ( GOOS=linux staticcheck -f binary ./... GOOS=windows staticcheck -f binary ./... GOOS=darwin staticcheck -f binary ./... ) | staticcheck -merge ``` ```sh # Method 3: -matrix flag (declarative, single invocation) staticcheck -matrix < ``` -------------------------------- ### Staticcheck -debug.version Flag Output Source: https://github.com/dominikh/go-tools/blob/master/website/content/changes/2019.2.md Shows detailed version information for staticcheck, including the Go version used for compilation and dependency versions. Intended for debugging issues. ```text $ staticcheck -debug.version staticcheck (devel, v0.0.0-20190602125119-5a4a2f4a438d) Compiled with Go version: go1.12.5 Main module: شان.co/go/tools@v0.0.0-20190602125119-5a4a2f4a438d (sum: h1:U5vSGN1Bjr0Yd/4pRcp8iRUCs3S5TIPzoAeTEFV2aiU=) Dependencies: github.com/BurntSushi/toml@v0.3.1 (sum: h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=) شان.org/x/tools@v0.0.0-20190530171427-2b03ca6e44eb (sum: h1:mnQlcVx8Qq8L70HV0DxUGuiuAtiEHTwF1gYJE/EL9nU=) ``` -------------------------------- ### Human-readable struct layout Source: https://context7.com/dominikh/go-tools/llms.txt Inspect the byte offset, size, and padding of each field in a Go struct. This command shows the layout of bufio.Reader. ```sh # Human-readable layout of bufio.Reader structlayout bufio Reader # Reader.buf []byte: 0-24 (24 bytes) # Reader.rd io.Reader: 24-40 (16 bytes) # Reader.r int: 40-48 (8 bytes) # Reader.w int: 48-56 (8 bytes) # Reader.err error: 56-72 (16 bytes) # Reader.lastByte int: 72-80 (8 bytes) # Reader.lastRuneSize int: 80-88 (8 bytes) ``` -------------------------------- ### Staticcheck Output with Build Tags Source: https://github.com/dominikh/go-tools/blob/master/website/content/docs/running-staticcheck/cli/build-tags/index.md Shows the output of Staticcheck when run with specific GOOS values, highlighting unused function warnings. ```terminal $ GOOS=linux staticcheck shared.go:4:6: func bar is unused (U1000) $ GOOS=windows staticcheck shared.go:3:6: func foo is unused (U1000) ``` -------------------------------- ### CI Workflow with Staticcheck Action Source: https://context7.com/dominikh/go-tools/llms.txt This workflow uses the staticcheck-action to perform static analysis on Go code. It demonstrates pinning to a specific version and overriding the minimum Go version. ```yaml name: "CI" on: ["push", "pull_request"] jobs: # Minimal job: Staticcheck only staticcheck: name: "Staticcheck" runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 1 - uses: dominikh/staticcheck-action@v1.2.0 with: version: "2025.1" # Pin to a specific release # min-go-version: "1.21" # Override minimum Go version (defaults to go.mod) # build-tags: "integration" # Pass -tags to staticcheck # install-go: true # Default: true; set false if job already has Go ``` -------------------------------- ### Pretty Print Struct Layout from JSON Source: https://github.com/dominikh/go-tools/blob/master/cmd/structlayout/README.md Use structlayout-pretty to visualize the JSON output from structlayout as an ASCII graphic, clearly showing field positions and padding. ```bash $ structlayout -json bufio Reader | structlayout-pretty +--------+ 0 | | <- Reader.buf []byte +--------+ -........- +--------+ 23 | | +--------+ 24 | | <- Reader.rd io.Reader +--------+ -........- +--------+ 39 | | +--------+ 40 | | <- Reader.r int +--------+ -........- +--------+ 47 | | +--------+ 48 | | <- Reader.w int +--------+ -........- +--------+ 55 | | +--------+ 56 | | <- Reader.err error +--------+ -........- +--------+ 71 | | +--------+ 72 | | <- Reader.lastByte int +--------+ -........- +--------+ 79 | | +--------+ 80 | | <- Reader.lastRuneSize int +--------+ -........- +--------+ 87 | | +--------+ ``` -------------------------------- ### Print Struct Layout Human-Readable Source: https://github.com/dominikh/go-tools/blob/master/cmd/structlayout/README.md Use structlayout to print the byte offset, size, and field names of a Go struct in a human-readable format. This is the default output when no flags are specified. ```bash $ structlayout bufio Reader Reader.buf []byte: 0-24 (24 bytes) Reader.rd io.Reader: 24-40 (16 bytes) Reader.r int: 40-48 (8 bytes) Reader.w int: 48-56 (8 bytes) Reader.err error: 56-72 (16 bytes) Reader.lastByte int: 72-80 (8 bytes) Reader.lastRuneSize int: 80-88 (8 bytes) ``` -------------------------------- ### GitHub Actions Integration (YAML) Source: https://context7.com/dominikh/go-tools/llms.txt Integrate Staticcheck into your GitHub Actions CI pipeline using the official action for automated analysis and caching. ```yaml ```