Skip to content

yyle88/formatgo

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

formatgo

Go source code formatting utilities with advanced import management and condensing capabilities.


CHINESE README

中文说明

Main Features

🎯 Multi-Stage Formatting: Standard formatting, import condensing, and automatic import management ⚡ Flexible Processing: Format bytes, strings, files, and entire DIR trees 🔄 Import Optimization: Remove blank lines from import blocks and organize imports 🌍 Customizable Filtering: Configure DIR and file filtering with predicates 📋 Type-Safe Options: Comprehensive configuration with sensible defaults

Installation

go get github.com/yyle88/formatgo

Usage

The package provides multiple functions to format Go code at different granularities.

FormatBytes

Formats Go source code from a byte slice.

formattedCode, err := formatgo.FormatBytes(code []byte)
  • code: The source code as a byte slice.
  • Returns the formatted code as a byte slice with an exception when formatting fails.

FormatCode

Formats Go source code from a string.

formattedCode, err := formatgo.FormatCode(code string)
  • code: The source code as a string.
  • Returns the formatted code as a string with an exception when formatting fails.

FormatFile

Formats a Go source code file at the given path.

err := formatgo.FormatFile(path string)
  • path: The path to the Go source code file.
  • Returns an exception when the formatting fails.

FormatRoot

Formats Go source files in the specified root DIR and its subdirectories.

err := formatgo.FormatRoot(root string)
  • root: The root DIR to start formatting files from.
  • Returns an exception when something happens during the formatting process.

Example

Here's a simple example showing how to format Go code from a string:

package main

import (
	"fmt"
	"github.com/yyle88/formatgo"
)

func main() {
	code := `package main

import "fmt"

func main() {fmt.Println("Hello, world!")}`

	formattedCode, err := formatgo.FormatCode(code)
	if err != nil {
		fmt.Println("Exception when formatting code:", err)
		return
	}

	fmt.Println("Formatted Code:", formattedCode)
}

Advanced Usage

Custom Options

Use FormatBytesWithOptions to customize the formatting process:

options := formatgo.NewOptions()
options.CondenseImport = true  // Remove blank lines from imports
options.IsFormatImport = true  // Enable automatic import management

formattedCode, err := formatgo.FormatBytesWithOptions(code, options)

DIR Formatting with Filters

Use FormatRootWithOptions to customize DIR processing:

options := formatgo.NewRootOptions()
options.FilterFile = func(depth int, path string, name string) bool {
    // Skip test files
    return !strings.HasSuffix(name, "_test.go")
}

err := formatgo.FormatRootWithOptions(root, options)

📄 License

MIT License. See LICENSE.


🤝 Contributing

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • 🐛 Found a mistake? Open an issue on GitHub with reproduction steps
  • 💡 Have a feature idea? Create an issue to discuss the suggestion
  • 📖 Documentation confusing? Report it so we can improve
  • 🚀 Need new features? Share the use cases to help us understand requirements
  • Performance issue? Help us optimize through reporting slow operations
  • 🔧 Configuration problem? Ask questions about complex setups
  • 📢 Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • 💬 Feedback? We welcome suggestions and comments

🔧 Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • Give GitHub stars if this project helps you
  • 🤝 Share with teammates and (golang) programming friends
  • 📝 Write tech blogs about development tools and workflows - we provide content writing support
  • 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! 🎉🎉🎉


GitHub Stars

Stargazers

About

`formatgo` is a Go package that provides utilities for formatting Go source code

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •