Find all functions not referenced in the Go project.

In a Go language project, using staticcheck to find unused functions is an efficient static analysis method.

1. Install staticcheck

Ensure that Go (version 1.16+) is installed, and then execute the following command to install staticcheck:

go install honnef.co/go/tools/cmd/staticcheck@latest

2. Basic Usage: Finding Unused Functions

Run the following command in the project root directory:

staticcheck ./...

Key Check Rules:

  • U1000: Detects unused functions, methods, variables, or types.
  • U1001: Detects unused parameters.

3. Filter Specific Check Rules

If you only want to check unused functions, you can specify the rules:

staticcheck -checks=U1000 ./...

4. Output Format

Default output format is {path}:{line}:{column}: {message}, for example:

main.go:10:2: func UnusedFunction is unused (U1000)

5. Configuration File (Optional)

Create a .staticcheck.conf file in the project root directory to customize inspection rules:

{
  "checks": ["U1000", "-ST1000"]  // Enable U1000, disable ST1000 (string formatting rule)
}

6. Integration with VS Code

  1. Install the Go Extension.
  2. Add to settings.json:

7. Ignore Specific Code

Adding a comment //lint:ignore U1000 reason above the function to ignore the check:

//lint:ignore U1000 Used by generated code
func UnusedButNeeded() {}

Frequently Asked Questions

  • Q: How do I handle unused functions in test files?
  • A: staticcheck defaults to checking test files. To exclude them, use the -tests=false flag.
  • Q: How do I integrate it into CI/CD?
  • A: Add to GitHub Actions:

Example Output

$ staticcheck -checks=U1000 ./...
internal/utils/helper.go:15:2: func privateHelper is unused (U1000)
cmd/server/main.go:23:2: func initConfig is unused (U1000)

Using the staticcheck rule U1000, you can quickly identify and clean up unused functions, improving code quality.

Licensed under CC BY-NC-SA 4.0
Last updated on Jun 02, 2025 21:13
A financial IT programmer's tinkering and daily life musings
Built with Hugo
Theme Stack designed by Jimmy