Go-Field is a powerful tool designed for Golang developers to enhance code readability and optimize memory usage by performing multi-level field alignment in struct declarations while preserving original metadata.
- Analyzes struct field alignment and padding in Go source files
- Calculates the size and alignment of each struct and its fields
- Optimizes struct layout by reordering fields for better memory efficiency
- Performs multi-level struct field alignment for improved readability
- Preserves original comments and metadata
- Supports nested structs and complex type hierarchies
- Processes single files or entire directories
- Offers an option to automatically apply optimizations to source files
- Easily integrates with existing Go projects
- Provides debug mode for detailed analysis
- Supports custom file pattern matching and ignoring
To install gofield
, make sure you have Go installed on your system, then run:
# get all versions
go list -m -versions github.com/t34-dev/go-field-alignment
# get package
go get -u github.com/t34-dev/go-field-alignment/v2@latest
# install package
go install github.com/t34-dev/go-field-alignment/v2/cmd/gofield@latest
or
go install github.com/t34-dev/go-field-alignment/v2/cmd/gofield@v2.0.3
For local installation:
# Bash
go build -o $GOPATH/bin/gofield # Unix
go build -o $GOPATH/bin/gofield.exe # Windows
# Makefile
make install # Any system
gofield [options] <file or directory paths>
--files
,-f
: Comma-separated list of files or folders to process (required)--ignore
,-i
: Comma-separated list of files or folders to ignore--view
,-v
: Print the absolute paths of found files--fix
: Make changes to the files--pattern
: Regex pattern for files to process (default:\.go$
)--ignore-pattern
: Regex pattern for files to ignore--version
: Print the version of the program--help
: Print usage information--debug
: Enable debug mode
-
Analyze all Go files in the current directory:
gofield --files .
-
Optimize structs in specific files:
gofield --files main.go,utils.go --fix
-
Process files matching a custom pattern:
gofield --files src --pattern "\\.(go|proto)$"
-
Ignore test files:
gofield --files . --ignore-pattern "_test\\.go$"
-
View files that would be processed without making changes:
gofield --files src,pkg --view gofield --files "./internal/models/, ./cmd/" --view
-
Process multiple directories while ignoring specific folders:
gofield --files "gofield,internal" --ignore "internal/generated"
-
Use debug mode for detailed analysis:
gofield --files main.go --debug
-
Combine multiple options:
gofield --files "src,pkg" --ignore "pkg/generated" --pattern "\\.(go|pb\\.go)$" --fix --view
For each struct found in the processed files, gofield
will output:
- Struct name
- Total size of the struct (before and after optimization)
- Alignment of the struct
- For each field:
- Field name
- Field type
- Offset within the struct
- Size of the field
- Alignment of the field
If the --fix
option is used, it will also show the optimized layout of the struct and apply the changes to the source files.
When using the --debug
option, Go-Field provides a detailed before-and-after comparison of struct layouts.
- Go-Field parses the specified Go source files and identifies all struct declarations.
- It analyzes the current layout of each struct, calculating sizes, alignments, and paddings.
- The tool then optimizes the struct layout by reordering fields to minimize padding while maintaining correct alignment.
- If the
--fix
option is used, Go-Field rewrites the struct declarations in the source files with the optimized layout. - The tool preserves all comments and formatting to maintain code readability.
- Run Go-Field on your project before committing changes to ensure optimal struct layouts.
- Use the
--view
option to preview which files would be affected before applying fixes. - Integrate Go-Field into your CI/CD pipeline to catch suboptimal struct layouts early.
- When optimizing performance-critical code, use Go-Field in conjunction with benchmarking to measure the impact of struct optimizations.
Contributions are welcome! Please feel free to submit a Pull Request. Here are some ways you can contribute:
- Improve documentation
- Add new features
- Fix bugs
- Optimize performance
- Write tests
Before submitting a pull request, please ensure your code passes all tests and adheres to the project's coding standards.
This project is licensed under the ISC License. See the LICENSE file for details.
If you encounter any issues or have questions, please file an issue on the GitHub repository.
Developed with ❤️ by T34