{"id":22542015,"url":"https://github.com/t34-dev/go-field-alignment","last_synced_at":"2025-04-09T22:42:57.940Z","repository":{"id":254015944,"uuid":"843779164","full_name":"t34-dev/go-field-alignment","owner":"t34-dev","description":"A powerful tool designed for Golang developers to enhance code readability by performing multi-level field alignment in struct declarations while preserving original metadata.","archived":false,"fork":false,"pushed_at":"2024-10-08T15:45:55.000Z","size":260,"stargazers_count":46,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-20T03:05:09.644Z","etag":null,"topics":["alignment","cicd","clean","cli","field","go","optimization","padding","structure"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/t34-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-17T11:49:50.000Z","updated_at":"2025-03-17T09:04:21.000Z","dependencies_parsed_at":"2024-12-23T19:38:59.994Z","dependency_job_id":null,"html_url":"https://github.com/t34-dev/go-field-alignment","commit_stats":null,"previous_names":["t34-dev/go-padding","t34-dev/gopad","t34-dev/go-pad","t34-dev/go-field-alignment","t34-dev/go-pad-alignment"],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-field-alignment","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-field-alignment/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-field-alignment/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/t34-dev%2Fgo-field-alignment/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/t34-dev","download_url":"https://codeload.github.com/t34-dev/go-field-alignment/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248124958,"owners_count":21051758,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alignment","cicd","clean","cli","field","go","optimization","padding","structure"],"created_at":"2024-12-07T13:08:19.134Z","updated_at":"2025-04-09T22:42:57.920Z","avatar_url":"https://github.com/t34-dev.png","language":"Go","readme":"# Go-Field\n\n[![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)\n[![Coverage Status](https://coveralls.io/repos/github/t34-dev/go-field-alignment/badge.svg?branch=main\u0026ver=1728402353)](https://coveralls.io/github/t34-dev/go-field-alignment?branch=main\u0026ver=1728402353)\n![Go Version](https://img.shields.io/badge/Go-1.22-blue?logo=go\u0026ver=1728402353)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/t34-dev/go-field-alignment?ver=1728402353)\n![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/t34-dev/go-field-alignment?sort=semver\u0026style=flat\u0026logo=git\u0026logoColor=white\u0026label=Latest%20Version\u0026color=blue\u0026ver=1728402353)\n\nGo-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.\n\n![Go-Field Example](./example.png)\n![Go-Field Example2](./example_view.png)\n![Go-Field Example3](./example_ignore.png)\n\n## Features\n\n- Analyzes struct field alignment and padding in Go source files\n- Calculates the size and alignment of each struct and its fields\n- Generics support\n- single-pass iteration \n- flexible configuration of files and directories for search \n- flexible configuration of ignore files (full control)\n- Optimizes struct layout by reordering fields for better memory efficiency\n- Performs multi-level struct field alignment for improved readability\n- Preserves original comments and metadata\n- Supports nested structs and complex type hierarchies\n- Processes single files or entire directories\n- Offers an option to automatically apply optimizations to source files\n- Easily integrates with existing Go projects\n- Provides debug mode for detailed analysis\n- Supports custom file pattern matching and ignoring\n\n## Installation\n\nTo install `gofield`, make sure you have Go installed on your system, then run:\n\n```shell\n# get all versions\ngo list -m -versions github.com/t34-dev/go-field-alignment/v2\n\n# get package\ngo get -u github.com/t34-dev/go-field-alignment/v2@latest\n\n# install package\ngo install github.com/t34-dev/go-field-alignment/v2/cmd/gofield@latest\nor\ngo install github.com/t34-dev/go-field-alignment/v2/cmd/gofield@v2.0.3\n```\n\nFor local installation:\n\n```shell\n# Bash\ngo build -o $GOPATH/bin/gofield       # Unix\ngo build -o $GOPATH/bin/gofield.exe   # Windows\n\n# Makefile\nmake install                        # Any system\n```\n\n## Usage\n\n```\ngofield [options] \u003cfile or directory paths\u003e\n```\n\n### Options\n\n- `--files`, `-f`: Comma-separated list of files or folders to process (required)\n- `--ignore`, `-i`: Comma-separated list of files or folders to ignore\n- `--view`, `-v`: Print the absolute paths of found files\n- `--fix`: Make changes to the files\n- `--pattern`: Regex pattern for files to process (default: `\\.go$`)\n- `--ignore-pattern`: Regex pattern for files to ignore\n- `--version`: Print the version of the program\n- `--help`: Print usage information\n- `--debug`: Enable debug mode\n\n### Examples\n\n1. Analyze all Go files in the current directory:\n   ```\n   gofield --files .\n   ```\n\n2. Optimize structs in specific files:\n   ```\n   gofield --files main.go,utils.go --fix\n   ```\n\n3. Process files matching a custom pattern (default: `\\.go$`):\n   ```\n   gofield --files src --pattern \"\\\\.(go|proto)$\"\n   ```\n\n4. Ignore test files:\n   ```\n   gofield --files . --ignore-pattern \"_test\\\\.go$\"\n   ```\n\n5. View files that would be processed without making changes:\n   ```\n   gofield --files src,pkg --view\n   gofield --files \"./internal/models/, ./cmd/\" --view\n   ```\n\n6. Process multiple directories while ignoring specific folders:\n   ```\n   gofield --files \"gofield,internal\" --ignore \"internal/generated\"\n   ```\n\n7. Use debug mode for detailed analysis:\n   ```\n   gofield --files main.go --debug\n   ```\n\n8. Combine multiple options:\n   ```\n   gofield --files \"src,pkg\" --ignore \"pkg/generated\" --pattern \"\\\\.(go|pb\\\\.go)$\" --fix --view\n   ```\n\n9. Ignore all test files + one special file.:\n   ```\n   gofield -f . --ignore-pattern \"file\\.go|_test\\.go$\"\n   ```\n\n## Output\n\nFor each struct found in the processed files, `gofield` will output:\n\n- Struct name\n- Total size of the struct (before and after optimization)\n- Alignment of the struct\n- For each field:\n  - Field name\n  - Field type\n  - Offset within the struct\n  - Size of the field\n  - Alignment of the field\n\nIf the `--fix` option is used, it will also show the optimized layout of the struct and apply the changes to the source files.\n\nWhen using the `--debug` option, Go-Field provides a detailed before-and-after comparison of struct layouts.\n\n## How It Works\n\n1. Go-Field parses the specified Go source files and identifies all struct declarations.\n2. It analyzes the current layout of each struct, calculating sizes, alignments, and paddings.\n3. The tool then optimizes the struct layout by reordering fields to minimize padding while maintaining correct alignment.\n4. If the `--fix` option is used, Go-Field rewrites the struct declarations in the source files with the optimized layout.\n5. The tool preserves all comments and formatting to maintain code readability.\n\n## Best Practices\n\n- Run Go-Field on your project before committing changes to ensure optimal struct layouts.\n- Use the `--view` option to preview which files would be affected before applying fixes.\n- Integrate Go-Field into your CI/CD pipeline to catch suboptimal struct layouts early.\n- When optimizing performance-critical code, use Go-Field in conjunction with benchmarking to measure the impact of struct optimizations.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request. Here are some ways you can contribute:\n\n- Improve documentation\n- Add new features\n- Fix bugs\n- Optimize performance\n- Write tests\n\nBefore submitting a pull request, please ensure your code passes all tests and adheres to the project's coding standards.\n\n## License\n\nThis project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.\n\n## Support\n\nIf you encounter any issues or have questions, please file an issue on the [GitHub repository](https://github.com/t34-dev/go-field-alignment/issues).\n\n---\n\nDeveloped with ❤️ by [T34](https://github.com/t34-dev)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft34-dev%2Fgo-field-alignment","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ft34-dev%2Fgo-field-alignment","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ft34-dev%2Fgo-field-alignment/lists"}