formatgo

package module
v1.0.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2025 License: MIT Imports: 14 Imported by: 7

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!!!

Starring

starring

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanCodeImportNewlines

func CleanCodeImportNewlines(source []byte) ([]byte, error)

CleanCodeImportNewlines processes the source code and condenses consecutive empty lines in the import section to a single newline. CleanCodeImportNewlines 处理源代码,并将导入部分中的连续空行压缩为单一的换行符。

func CleanFileImportNewlines

func CleanFileImportNewlines(path string) error

CleanFileImportNewlines reads a Go source file, condenses the import section by removing consecutive empty lines, and writes the result back to the file. CleanFileImportNewlines 读取 Go 源代码文件,压缩导入部分 通过去除连续的空行并将结果写回文件。

func FormatBytes

func FormatBytes(code []byte) ([]byte, error)

FormatBytes formats Go source code from a byte slice. FormatBytes 格式化golang的源代码

func FormatBytesWithOptions

func FormatBytesWithOptions(code []byte, options *Options) ([]byte, error)

FormatBytesWithOptions formats Go source code from a byte slice. Even if an error occurs during formatting, it returns the intermediate code. FormatBytesWithOptions 格式化 Go 的源代码(以字节切片形式提供)。 即使在格式化期间发生错误,也会返回中间结果代码。

func FormatCode

func FormatCode(code string) (string, error)

FormatCode formats Go source code from a string. FormatCode 格式化源代码字符串

func FormatCodeWithOptions

func FormatCodeWithOptions(code string, options *Options) (string, error)

FormatCodeWithOptions formats Go source code from a string. Even if an error occurs during formatting, it returns the intermediate code as a string. FormatCodeWithOptions 格式化 Go 的源代码(以字符串形式提供)。 即使在格式化期间发生错误,也会返回中间结果代码(字符串形式)。

func FormatFile

func FormatFile(path string) error

FormatFile formats a Go source code file at the given path. FormatFile 格式化源代码文件

func FormatFileWithOptions

func FormatFileWithOptions(path string, options *Options) error

FormatFileWithOptions formats a Go source code file. It reads the file, formats it, and writes back the result if changes are made. FormatFileWithOptions 格式化一个 Go 源代码文件。 它会读取文件内容,进行格式化,如果内容有变化则写回结果。

func FormatRoot

func FormatRoot(root string) error

FormatRoot formats all Go source files in the specified root directory and its subdirectories. FormatRoot 格式化整个目录以及其子目录下的所有go文件

func FormatRootWithOptions

func FormatRootWithOptions(root string, options *RootOptions) error

FormatRootWithOptions formats all Go files in a directory and its subdirectories. It recursively processes each directory and applies the provided options. FormatRootWithOptions 格式化指定目录及其子目录下的所有 Go 文件。 它递归处理每个目录,并根据提供的选项进行格式化。

func NewImportsOptions

func NewImportsOptions() *imports.Options

NewImportsOptions returns a new imports.Options with default settings. NewImportsOptions 返回一个具有默认设置的 imports.Options。

Types

type Options

type Options struct {
	ImportsOptions *imports.Options // Options for formatting imports  // 导入格式化的选项
	CondenseImport bool             // Whether to condense imports by removing empty lines // 是否压缩导入部分,去除空行
	IsFormatImport bool             // Whether to format import statements // 是否格式化导入语句
}

Options holds configuration options for imports formatting. Options 结构体保存了导入格式化的配置选项。

func NewOptions

func NewOptions() *Options

NewOptions creates and returns a new Options instance with default values. NewOptions 创建并返回一个带有默认值的 Options 实例。

type RootOptions

type RootOptions struct {
	FileOptions     *Options                                       // File formatting options // 文件格式化选项
	FilterRoot      func(depth int, path string, name string) bool // Filter to format only directories that match certain criteria // 用于过滤目录名/路径的条件,只有符合条件的目录才会被格式化
	FilterFile      func(depth int, path string, name string) bool // Filter to format only files that match certain criteria // 用于过滤文件名/路径的条件,只有符合条件的文件才会被格式化
	FileHasSuffixes []string                                       // Suffixes of files to be formatted // 要格式化的文件后缀列表
	SkipHiddenDepth int                                            // Depth level to skip hidden directories (e.g., .git, .idea) // 在多少层深度内跳过隐藏目录(例如 .git,.idea)
}

RootOptions holds configuration options for the root directory formatting. RootOptions 结构体保存了根目录格式化的配置选项。

func NewRootOptions

func NewRootOptions() *RootOptions

NewRootOptions creates and returns a new RootOptions instance with default values. NewRootOptions 创建并返回一个带有默认值的 RootOptions 实例。

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL