tools

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangePkgName

func ChangePkgName(code string, pkgname string) string

ChangePkgName change package name of the code and return the new code.

func CodeStyleType

func CodeStyleType(typeString string) string

CodeStyleType converts *a/b/c.T to *c.T

func Format

func Format(filename string, src interface{}, opt *Options) ([]byte, error)

Format formats and adjusts imports for the provided file. If opt is nil the defaults are used.

Note that filename's directory influences which imports can be chosen, so it is important that filename be accurate. To process data “as if” it were in filename, pass the data as a non-nil src.

func FormatFile

func FormatFile(filename string, content []byte, opt *Options) error

FormatFile formats and adjusts imports for the provided file, and rewrites it. NOTE:

If content is not empty, use it as new content, format it and overwriting the original file content.

func MkdirAll

func MkdirAll(path string, perm ...os.FileMode) error

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil. If perm is empty, default use 0755.

func PkgName

func PkgName(filenameOrDirectory string, src interface{}) (string, error)

PkgName get the package name of the code, file or directory. NOTE:

If src==nil, find the package name from the file or directory specified by 'filenameOrDirectory';
If src!=nil, find the package name from the code represented by 'src'.

func ReadSource

func ReadSource(filename string, src interface{}) ([]byte, error)

func ReadSourceBytes

func ReadSourceBytes(src interface{}) ([]byte, error)

func ReplaceFile

func ReplaceFile(fset *token.FileSet, node ast.Node, newCode string) error

ReplaceFile replaces the bytes selected by [start, end] with the new content.

func RewriteFile

func RewriteFile(name string, fn func(content []byte) (newContent []byte, err error)) error

RewriteFile rewrite file.

func WriteFile

func WriteFile(filename string, data []byte, perm ...os.FileMode) error

WriteFile write file, and automatically creates the directory if necessary. NOTE:

If perm is empty, automatically determine the file permissions based on extension.

Types

type Options

type Options = imports.Options

Options specifies options for processing files.

type Options struct {
	Fragment  bool // Accept fragment of a source file (no package statement)
	AllErrors bool // Report all errors (not just the first 10 on different lines)
	Comments  bool // Print comments (true if nil *Options provided)
	TabIndent bool // Use tabs for indent (true if nil *Options provided)
	TabWidth  int  // Tab width (8 if nil *Options provided)
	FormatOnly bool // Disable the insertion and deletion of imports
}

Jump to

Keyboard shortcuts

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