formatgo
is a Go package that provides utilities for formatting Go source code, whether it's in a byte slice, string, or a file, and even for entire directories containing Go files.
To install the formatgo
package, you can run the following command:
go get github.com/yyle88/formatgo
The package provides several functions for formatting Go code. Below are the main functions that you can use:
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 or an error if something goes wrong.
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 or an error if something goes wrong.
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 error if the formatting fails.
Formats all Go source files in the specified root directory and its subdirectories.
err := formatgo.FormatRoot(root string)
root
: The root directory to start formatting files from.- Returns an error if something goes wrong during the formatting process.
Here’s a simple example of 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("Error formatting code:", err)
return
}
fmt.Println("Formatted Code:", formattedCode)
}
MIT License. See LICENSE.
Contributions are welcome! To contribute:
- Fork the repo on GitHub (using the webpage interface).
- Clone the forked project (
git clone https://github.com/yourname/repo-name.git
). - Navigate to the cloned project (
cd repo-name
) - Create a feature branch (
git checkout -b feature/xxx
). - Stage changes (
git add .
) - Commit changes (
git commit -m "Add feature xxx"
). - Push to the branch (
git push origin feature/xxx
). - Open a pull request on GitHub (on the GitHub webpage).
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project by submitting pull requests and reporting issues.
If you find this package valuable, give me some stars on GitHub! Thank you!!!
Thank you for your support!
Happy Coding with this package! 🎉
Give me stars. Thank you!!!