A lightweight command-line tool written in Go to recursively count lines of Go source code across a project.
Displays line counts for individual files, sorted in ascending order, followed by the total number of files and lines.
Built entirely with the Go standard library, with no external dependencies.
- Recursively scans directories for
.gofiles. - Displays line counts per file, sorted from smallest to largest.
- Excludes Go test files (
_test.go) by default. - Excludes vendor directories (
vendor) by default. - Excludes Protocol Buffers generated files (
.pb.go) by default. - Optionally ignores empty and whitespace-only lines.
- Supports excluding multiple directories by name or relative path.
- Automatically skips hidden directories such as
.git. - Supports specifying a custom root directory.
- Handles files without trailing newline characters.
- Uses buffered file reading for efficient processing.
- Requires no third-party dependencies.
- Go 1.27.3 or later.
- Compatible with Linux, macOS, and Windows.
Clone the repository:
git clone https://github.com/wasmup/golines.git
cd golinesBuild the executable:
go build -o golines .Optionally install it into your Go binary directory:
go install
# or
go install -trimpath -ldflags=-sgolines [flags]By default, the tool scans the current directory and its subdirectories, excluding test files, vendor directories, protobuf-generated files, and hidden directories.
| Flag | Default | Description |
|---|---|---|
-dir |
. |
Root directory to scan |
-t |
false |
Include _test.go files |
-v |
false |
Include vendor directories |
-p |
false |
Include .pb.go files |
-s |
false |
Skip empty and whitespace-only lines |
-x |
None | Exclude directories by name or relative path |
-h |
— | Display help |
Count Go source lines in the current directory
golinesExclude empty and whitespace-only lines
golines -sInclude test files
golines -tInclude vendor directories
golines -vInclude protobuf-generated files
golines -pInclude all Go files
golines -t -v -pScan a specific directory
golines -dir ./internalExclude multiple directories
golines -x mocks generated tmpExclude directories using relative paths
golines -x internal/generated pkg/mocksCombine multiple options
golines -dir ./src -s -t -x mocks generatedImportant: The
-xflag must be the last flag in the command. Every argument following-xis interpreted as an excluded directory.
12 internal/config.go
38 internal/errors.go
94 cmd/server/main.go
127 internal/logger.go
285 internal/database.go
412 internal/handler.go
------------------------------------------------------------
Files: 6
Total lines: 968
Files are sorted by line count in ascending order. Files with identical line counts are sorted alphabetically by path.
The -x flag supports two matching strategies.
golines -x mocksExcludes all directories named mocks, regardless of their location:
project/
├── internal/
│ ├── mocks/ # Excluded
│ └── service/
├── pkg/
│ └── mocks/ # Excluded
└── mocks/ # Excluded
golines -x internal/mocksExcludes only internal/mocks and its descendants.
Other directories named mocks remain included.
Directory paths are evaluated relative to the root directory specified by -dir.
Unless explicitly enabled, the following are excluded:
| Type | Pattern | Enable with |
|---|---|---|
| Go tests | *_test.go |
-t |
| Vendor directories | vendor/ |
-v |
| Protobuf files | *.pb.go |
-p |
| Hidden directories | .*/ |
Always excluded |
The tool counts physical lines rather than logical statements or expressions.
For example:
package main
import "fmt"
func main() {
// Print a message
fmt.Println("Hello, World!")
}Results:
| Mode | Lines counted |
|---|---|
| Default | 8 |
With -s |
6 |
When -s is enabled, lines containing only spaces, tabs, newlines, or other Unicode whitespace are ignored.
Comment-only lines are still counted.
The tool uses:
filepath.WalkDirfor recursive directory traversal.bufio.Readerfor buffered file reading.unicode.IsSpacefor whitespace detection.sort.Slicefor ordering results.flagfor command-line argument parsing.
Each file is processed sequentially, without loading the entire file into memory.
Memory usage is primarily determined by the buffered reader and the number of file statistics retained for sorting.
- Counts physical lines, not logical lines of code.
- Does not exclude comments.
- Does not distinguish executable code from declarations.
- Hidden directories are always skipped, except when the hidden directory itself is selected as the root.
-xmust appear last.- Symbolic links to directories are not followed.
- Symbolic links and other non-regular files are not counted.
- File reading and traversal errors terminate execution.
The MIT License.
Contributions, bug reports, and feature requests are welcome. Open a GitHub issue or submit a pull request.