Categorygithub.com/yyle88/formatgo
modulepackage
1.0.21
Repository: https://github.com/yyle88/formatgo.git
Documentation: pkg.go.dev

# README

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

formatgo

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.

CHINESE README

中文说明

Installation

To install the formatgo package, you can run the following command:

go get github.com/yyle88/formatgo

Usage

The package provides several functions for formatting Go code. Below are the main functions that you can use:

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 or an error if something goes wrong.

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 or an error if something goes wrong.

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 error if the formatting fails.

FormatRoot

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.

Example

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)
}

License

formatgo is open-source and released under the MIT License. See the LICENSE file for more information.


Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

Thank you for your support!

Happy Coding with formatgo! 🎉

Give me stars. Thank you!!!

See stars

see stars

# Functions

CleanCodeImportNewlines processes the source code and condenses consecutive empty lines in the import section to a single newline.
CleanFileImportNewlines reads a Go source file, condenses the import section by removing consecutive empty lines, and writes the result back to the file.
FormatBytes formats Go source code from a byte slice.
FormatBytesWithOptions formats Go source code from a byte slice.
FormatCode formats Go source code from a string.
FormatCodeWithOptions formats Go source code from a string.
FormatFile formats a Go source code file at the given path.
FormatFileWithOptions formats a Go source code file.
FormatRoot formats all Go source files in the specified root directory and its subdirectories.
FormatRootWithOptions formats all Go files in a directory and its subdirectories.
NewImportsOptions returns a new imports.Options with default settings.
NewOptions creates and returns a new Options instance with default values.
NewRootOptions creates and returns a new RootOptions instance with default values.

# Structs

Options holds configuration options for imports formatting.
RootOptions holds configuration options for the root directory formatting.