This is a small utility that finds unused exported Go symbols (functions, methods ...) in Go

Go

This is a small utility that finds unused exported Go symbols (functions, methods ...) in Go. For all other similar use cases, use https://github.com/dominikh/go-tools

I have used this in Hugo (a monorepo with many packages), and it works, but there are some caveats:

  • It does not detect references from outside of your project.
  • It does not detect references via reflect.
  • Some possible surprises when it comes to interfaces.

So, you should inspect and test the proposed deletes. See this test repo for more information.

Install

go install github.com/bep/punused@latest

You also need gopls:

go install golang.org/x/tools/gopls@latest

Use

punused takes only one (optional) argument: A Glob filenam pattern (Unix style slashes, double asterisk is supported) of Go files to check.

punused needs to be run from the root of a Go Module. To test a specific package you can target it with a Glob, e.g. punused **/utils/*.go.

Running punused in this repository currently gives:

punused                                                                
internal/lib/gopls.go:125:2 field Detail is unused (EU1002)
internal/lib/gopls.go:135:2 field Tags is unused (EU1002)
internal/lib/gopls.go:141:2 field Deprecated is unused (EU1002)
internal/lib/gopls.go:147:2 field Range is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:7:2 variable UnusedVar is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:12:2 constant UnusedConst is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:19:6 function UnusedFunction is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:25:2 field UnusedField is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:32:15 method (MyType).UnusedMethod is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:36:6 interface UnusedInterfaceWithUsedAndUnusedMethod is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:38:2 method UnusedInterfaceMethodReturningInt is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:37:2 method UsedInterfaceMethodReturningInt is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:41:6 interface UnusedInterface is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:42:2 method UnusedInterfaceReturningInt is unused (EU1002)
internal/lib/testpackages/firstpackage/code1.go:45:6 interface UsedInterface is unused (EU1002)
internal/lib/testpackages/firstpackage/testlib1.go:4:2 constant OnlyUsedInTestConst is used in test only (EU1001)

Note that we currently skip checking test code, but you do warned about unused symbols only used in tests (see example above).

Owner
Bjørn Erik Pedersen
Software Developer and Saxophonist from Norway • Creator of @gohugoio
Bjørn Erik Pedersen
Similar Resources

Small utility to cleanup entries with unexpected/erroneous ttl in redis

Small utility to cleanup entries with unexpected/erroneous ttl in redis

Apr 28, 2022

Golang 1.18+ Generics implementation of Set methods

Golang Generics: Set A golang 1.18+ implementation of Set using Go generics Installation $ go get -u github.com/chrispappas/golang-generics-set Quick

Oct 26, 2022

This is an open source project for commonly used functions for the Go programming language.

Common Functions This is an open source project for commonly used functions for the Go programming language. This package need = go 1.3 Code Conventi

Jan 8, 2023

Use Golang to implement PHP's common built-in functions.

PHP2Go Use Golang to implement PHP's common built-in functions. About 140+ functions have been implemented. Install go get github.com/syyongx/php2go R

Dec 28, 2022

randstr is a module that contains functions for generating random strings.

randstr is a module that contains functions for generating random strings. The functions in this module uses the crypto/rand package. Installa

Nov 13, 2021

Go linter that warns about number of arguments in functions.

argslen linter Argslen is a linter that checks for long list of argument in functions. The default limit is 5 (maxArguments) and skip the test files (

Sep 17, 2022

The one-stop shop for most common Go functions

The one-stop shop for most common Go functions

Pandati The one stop shop for most common Go functions Table of contents Pandati The one stop shop for most common Go functions Table of contents Purp

Mar 21, 2022

Extended library functions using generics in Go.

Just few extended standard library functions for Golang using generics.

Dec 16, 2021

Simple library to handle ANSI functions and parsing of color formatting strings

Emerald A basic color library for use in my Go projects, built on top of mgutz/ansi. Package ansi is a small, fast library to create ANSI colored stri

Oct 28, 2022
A simple tool to auto remove unused imports in rust

rust_strip A simple tool to remove unused imports in RUST. Call Cargo build/test Process the warning logs of unused imports Then replace the involved

Oct 15, 2022
A fast unused and duplicate dependency checker
A fast unused and duplicate dependency checker

Depp - A fast unused and duplicate package checker Installation npm install -g depp-installer (will try to get npm install -g depp later) Usage Just

Oct 31, 2022
Clean up unused AWS resources

vacuum Vacuum your AWS account of unused resources to save you ?? ?? !! To run: vacuum all ▒▒▒▒▒▒▒▒▒▒▒▒

Aug 10, 2022
Robust & Easy to use struct mapper and utility methods for Go

go-model Robust & Easy to use model mapper and utility methods for Go struct. Typical methods increase productivity and make Go development more fun ?

Dec 30, 2022
A super simple Lodash like utility library with essential functions that empowers the development in Go
A super simple Lodash like utility library with essential functions that empowers the development in Go

A simple Utility library for Go Go does not provide many essential built in functions when it comes to the data structure such as slice and map. This

Jan 4, 2023
Utility functions for work with the Kubernetes Go-Client

go-k8s-utils This repository contains utils for the work with Kubernetes, in specific with the go-client library. Testing This package contains utils

Dec 14, 2022
Some utility functions for generic types in Go.

GOUF - Utility Functions for generic types Go team released Go 1.18 beta recently with support for Generics(a.k.a type parameters). This package provi

Apr 13, 2022
Protoc-gen-fieldmask - Generate FieldMask utility functions for protobuf

protoc-gen-fieldmask Generate FieldMask utility functions for protobuf Generated

Aug 20, 2022
Small utility to allow simpler, quicker testing of parsing files in crowdsec

cs_parser_test Small utility to allow simpler, quicker testing of parsing files in crowdsec Usage $ sudo cs_parser_test -t syslog /var/log/mail.log N

Jul 13, 2021
A small utility to extract your Telegram contacts as VCF file.

Telegram to VCF A small utility to extract your Telegram contacts as VCF file. Usage At first build this application or use the precompiled binaries o

Nov 16, 2022