A Go tool that finds duplicated string literals in Go source files.
.
├── cmd/
│ └── gorefactor/ # Main application entry point
│ └── main.go
├── pkg/
│ └── duplicates/ # Core logic for finding duplicated literals
│ ├── duplicates.go
│ └── duplicates_test.go
├── Makefile # Build automation
└── go.mod
Build the tool:
make buildOr using go directly:
go build -o gorefactor ./cmd/gorefactorRun it on a Go file:
./gorefactor <path-to-go-file>Example:
./gorefactor cmd/gorefactor/main.go
./gorefactor pkg/duplicates/duplicates.gomake build- Build the gorefactor binary (default)make clean- Remove the binarymake test- Run testsmake install- Install to GOPATH/bin
Run the test suite:
make testOr using go directly:
go test -v ./...The test suite includes:
- Finding duplicates in test files
- Handling files with no duplicates
- Error handling for invalid files
- Error handling for invalid Go code
- Multiple duplicate detection
The tool:
- Parses the provided Go file into an AST using Go's standard library (
pkg/duplicates) - Walks the AST to collect all string literals and their line numbers
- Identifies literals that appear more than once (exact string match)
- Outputs the duplicated literals with their locations (
cmd/gorefactor)
Duplicate string literals found:
String: "Hello, World!"
Found 3 times at lines: [6 7 12]
String: "example"
Found 2 times at lines: [5 9]