1 unstable release
Uses new Rust 2024
| 0.1.0 | Oct 24, 2025 |
|---|
#160 in Value formatting
535KB
14K
SLoC
gofmt.rs
A Rust implementation of Go language formatting, providing fast and accurate Go source code parsing and formatting capabilities.
Features
- Complete Go Parser: Full implementation of Go language parsing with proper AST generation
- Accurate Formatting: Produces output identical to the official
gofmttool - High Performance: Optimized Rust implementation with benchmarking capabilities
- Comprehensive Testing: Extensive test suite with golden files covering all Go language constructs
- Tab-based Formatting: Proper alignment and indentation using tabs and spaces
- Comment Preservation: Maintains and properly formats all types of Go comments
Installation
Add this to your Cargo.toml:
[dependencies]
gofmt = "0.1.0"
Usage
Basic Formatting
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(())
}
Library Structure
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 rules
Testing
Run the test suite:
cargo test
Run benchmarks:
cargo bench
Test Data
The project includes an extensive test suite with over 200 test cases covering:
- Basic Go constructs: functions, variables, types, interfaces
- Complex formatting: composite literals, function calls, expressions
- Comment handling: line comments, block comments, documentation
- Go build directives: build tags and compiler directives
- Advanced features: generics, type parameters, complex expressions
- gofumpt compatibility: Additional formatting rules from gofumpt
Each test case consists of:
.inputfile: Unformatted Go source code.goldenfile: Expected formatted output
Performance
The formatter is designed for high performance and includes benchmark tests. Run benchmarks to see performance characteristics:
cargo bench
Project Structure
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
Contributing
Contributions are welcome! This project aims to maintain 100% compatibility with the official Go formatter. When adding features or fixing bugs:
- Ensure all existing tests pass
- Add test cases for new functionality
- Follow Rust best practices and the existing code style
- Run benchmarks to ensure no performance regressions
Dependencies
strum: Enum utilitiesunic-ucd-category: Unicode character categoriesanyhow: Error handlingthiserror: Error derivationunicode-width: Text width calculationslibm: Math functionscriterion: Benchmarking (dev dependency)
License
This project follows the same principles as the original Go formatter, focusing on consistent and deterministic code formatting.
Dependencies
~3MB
~54K SLoC