| Crates.io | gofmt |
| lib.rs | gofmt |
| version | 0.1.0 |
| created_at | 2025-10-24 06:35:13.476635+00 |
| updated_at | 2025-10-24 06:35:13.476635+00 |
| description | gofmt implemention |
| homepage | |
| repository | https://github.com/zjykzk/gofmt.rs.git |
| max_upload_size | |
| id | 1898071 |
| size | 553,617 |
A Rust implementation of Go language formatting, providing fast and accurate Go source code parsing and formatting capabilities.
gofmt toolAdd this to your Cargo.toml:
[dependencies]
gofmt = "0.1.0"
use gofmt::formatter::format;
fn main() -> Result<(), String> {
let go_source = r#"
package main
func main(){
fmt.Println("Hello, World!")
}
"#;
let formatted = format(go_source)?;
let result = String::from_utf8(formatted).unwrap();
println!("{}", result);
Ok(())
}
The library is organized into several key modules:
formatter: Main formatting logic and public APIparser: Go language parser implementationast: Abstract Syntax Tree definitions for Go constructsscanner: Lexical analysis and tokenizationsource: Source code management and line trackingtabwriter: Tab-based formatting and alignmenttokens: Token definitions and precedence rulesRun the test suite:
cargo test
Run benchmarks:
cargo bench
The project includes an extensive test suite with over 200 test cases covering:
Each test case consists of:
.input file: Unformatted Go source code.golden file: Expected formatted outputThe formatter is designed for high performance and includes benchmark tests. Run benchmarks to see performance characteristics:
cargo bench
src/
├── lib.rs # Library entry point
├── formatter.rs # Main formatting logic
├── parser.rs # Go language parser
├── ast.rs # AST node definitions
├── scanner.rs # Lexical analyzer
├── source.rs # Source management
├── tabwriter.rs # Tab formatting
└── tokens.rs # Token definitions
tests_data/ # Test cases with input/golden files
benches/ # Performance benchmarks
Contributions are welcome! This project aims to maintain 100% compatibility with the official Go formatter. When adding features or fixing bugs:
strum: Enum utilitiesunic-ucd-category: Unicode character categoriesanyhow: Error handlingthiserror: Error derivationunicode-width: Text width calculationslibm: Math functionscriterion: Benchmarking (dev dependency)This project follows the same principles as the original Go formatter, focusing on consistent and deterministic code formatting.