Documentation ¶
Index ¶
- Variables
- func FindGoWorkspaceRoot(dir string) (string, error)
- func FindModuleGoVersion(dir string) (string, error)
- func FindModuleRoot(dir string) (string, error)
- func FormatAndCleanGoFile(filePath string) error
- func FormatAndCleanGoSrc(src []byte) ([]byte, error)
- func GetModuleNameFromDir(dir string) (string, error)
- func GoFmt(packages string, opts ...GoFmtOption) error
- func GoGenerate(packages string, opts ...GoGenerateOption) error
- func GoGet(repo string, opts ...GoGetOption) error
- func GoInstall(repo string, opts ...GoInstallOption) error
- func GoModTidy(opts ...GoModTidyOption) error
- func ImportPathForDir(dir string) (string, error)
- func InsertAfterConst(src []byte, content, constName string) ([]byte, error)
- func InsertAfterStruct(src []byte, content, structName string) ([]byte, error)
- func InsertAfterStructMethod(src []byte, content, structName, methodName string) ([]byte, error)
- func InsertAfterVar(src []byte, content, varName string) ([]byte, error)
- func InsertBeforeConst(src []byte, content, constName string) ([]byte, error)
- func InsertBeforeStruct(src []byte, content, structName string) ([]byte, error)
- func InsertBeforeStructMethod(src []byte, content, structName, methodName string) ([]byte, error)
- func InsertBeforeVar(src []byte, content, varName string) ([]byte, error)
- func InsertCommentAfterImportBlock(src []byte, comment string) ([]byte, error)
- func InsertCommentBeforeImportBlock(src []byte, comment string) ([]byte, error)
- func InsertConst(src []byte, constName, constType, constValue string) ([]byte, error)
- func InsertContentAfterFunction(src []byte, content, functionName string) ([]byte, error)
- func InsertContentBeforeFunction(src []byte, content, functionName string) ([]byte, error)
- func InsertFunction(src []byte, functionName, params, results, functionBody string) ([]byte, error)
- func InsertImport(src []byte, importPath, alias string) ([]byte, error)
- func InsertStruct(src []byte, structName string) ([]byte, error)
- func InsertStructField(src []byte, structName, fieldName, fieldType, tag string) ([]byte, error)
- func InsertStructMethod(src []byte, structName, methodName, methodBody, receiverName string, ...) ([]byte, error)
- func InsertVar(src []byte, varName, varType, varValue string) ([]byte, error)
- func IsConstExists(src []byte, constName string) (bool, error)
- func IsFunctionExists(src []byte, functionName string) (bool, error)
- func IsGoWorkspace(dir string) (bool, error)
- func IsInterfaceExists(src []byte, interfaceName string) (bool, error)
- func IsStructExists(src []byte, structName string) (bool, error)
- func IsStructMethodExists(src []byte, structName, methodName string) (bool, error)
- func IsTypeExists(src []byte, typeName string) (bool, error)
- func IsVarExists(src []byte, varName string) (bool, error)
- func NameForDir(dir string) (string, error)
- func NormalizeVendor(pkgPath string) string
- func ParseGoWork(dir string) (*modfile.WorkFile, error)
- func RemoveConst(src []byte, constName string) ([]byte, error)
- func RemoveImport(src []byte, importPath string) ([]byte, error)
- func RemoveStruct(src []byte, structName string) ([]byte, error)
- func RemoveStructField(src []byte, structName, fieldName string) ([]byte, error)
- func RemoveUnusedImports(src []byte) ([]byte, error)
- func RemoveUnusedImportsByFile(filePath string) error
- func RemoveVar(src []byte, varName string) ([]byte, error)
- type GoFmtOption
- type GoGenerateOption
- func WithGoGenerateDir(dir string) GoGenerateOption
- func WithGoGenerateEnv(env []string) GoGenerateOption
- func WithGoGenerateN(n bool) GoGenerateOption
- func WithGoGenerateRun(run string) GoGenerateOption
- func WithGoGenerateStderr(stderr io.Writer) GoGenerateOption
- func WithGoGenerateStdout(stdout io.Writer) GoGenerateOption
- func WithGoGenerateTimeout(timeout time.Duration) GoGenerateOption
- func WithGoGenerateV(v bool) GoGenerateOption
- func WithGoGenerateX(x bool) GoGenerateOption
- type GoGetOption
- type GoInstallOption
- type GoModTidyOption
- type Import
- type Imports
- type Package
- func (p *Package) AddBuildTags(tags ...string) *Package
- func (p *Package) ClearCache()
- func (p *Package) GetPackageName(importPath string) (string, error)
- func (p *Package) LoadPackage(importPath string) (*packages.Package, error)
- func (p *Package) LoadPackages(importPaths ...string) ([]*packages.Package, error)
- func (p *Package) PackageCount() int
- func (p *Package) RunModTidy() error
- func (p *Package) SetPackageCachePrefix(prefix string) *Package
Constants ¶
This section is empty.
Variables ¶
var ErrGoWorkNotFound = fmt.Errorf("go.work not found")
Functions ¶
func FindGoWorkspaceRoot ¶ added in v0.1.5
FindGoWorkspaceRoot 查找并返回Go工作区的根目录
func FindModuleGoVersion ¶ added in v0.1.3
FindModuleGoVersion 获取模块的Go版本
func FindModuleRoot ¶ added in v0.1.5
FindModuleRoot 查找模块根目录
func FormatAndCleanGoFile ¶
FormatAndCleanGoFile 格式化Go文件并移除未使用的导入
func FormatAndCleanGoSrc ¶ added in v0.1.3
FormatAndCleanGoSrc 格式化Go源码并移除未使用的导入
func GetModuleNameFromDir ¶
GetModuleNameFromDir 从指定目录获取模块名称
func GoFmt ¶ added in v0.1.5
func GoFmt(packages string, opts ...GoFmtOption) error
GoFmt 格式化指定包 如果 packages 为空,则格式化当前目录下的所有go文件
func GoGenerate ¶ added in v0.1.5
func GoGenerate(packages string, opts ...GoGenerateOption) error
GoGenerate 生成指定包
func GoGet ¶ added in v0.1.3
func GoGet(repo string, opts ...GoGetOption) error
GoGet 获取指定包 如果 projectDir 为空,则使用当前目录 如果 updateDependencies 为 true,则更新依赖 如果 updateDependencies 为 false,则不更新依赖
func GoInstall ¶ added in v0.1.3
func GoInstall(repo string, opts ...GoInstallOption) error
GoInstall 安装指定包
func ImportPathForDir ¶
ImportPathForDir 获取指定目录的import path
func InsertAfterConst ¶ added in v0.1.3
InsertAfterConst 用Go AST解析在指定常量后插入内容
func InsertAfterStruct ¶ added in v0.1.3
type MyStruct struct {Name string} `) newSrc, err := InsertAfterStruct(src, "type MyStruct2 struct {Name string}", "MyStruct") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertAfterStructMethod ¶ added in v0.1.3
type MyStruct struct {Name string} `) newSrc, err := InsertAfterStructMethod(src, "func (m *MyStruct) MyMethod() {fmt.Println(\"Hello, World!\")}", "MyStruct", "MyMethod") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertAfterVar ¶ added in v0.1.3
InsertAfterVar 用Go AST解析在指定变量后插入内容
func InsertBeforeConst ¶ added in v0.1.3
InsertBeforeConst 用Go AST解析在指定常量前插入内容
func InsertBeforeStruct ¶ added in v0.1.3
type MyStruct struct {Name string} `) newSrc, err := InsertBeforeStruct(src, "type MyStruct2 struct {Name string}", "MyStruct") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertBeforeStructMethod ¶ added in v0.1.3
type MyStruct struct {Name string} `) newSrc, err := InsertBeforeStructMethod(src, "func (m *MyStruct) MyMethod() {fmt.Println(\"Hello, World!\")}", "MyStruct", "MyMethod") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertBeforeVar ¶ added in v0.1.3
InsertBeforeVar 用Go AST解析在指定变量前插入内容
func InsertCommentAfterImportBlock ¶ added in v0.1.3
InsertCommentAfterImportBlock 用Go AST解析在指定导入块后插入comment 使用示例
func main() { src := []byte(`package main import "fmt" `) newSrc, err := InsertCommentAfterImportBlock(src, "这是一个注释 \n 这是一个注释") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertCommentBeforeImportBlock ¶ added in v0.1.3
InsertCommentBeforeImportBlock 用Go AST解析在指定导入块前插入comment src: 源代码的字节切片 comment: 要插入的comment 返回修改后的源码字节切片和可能的错误 使用示例
func main() { src := []byte(`package main import "fmt" `) newSrc, err := InsertCommentBeforeImportBlock(src, "// 这是一个注释") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertConst ¶ added in v0.1.3
InsertConst 使用 Go AST 解析在源码中插入一个新的常量 src: 源代码的字节切片 constName: 常量名称 constType: 常量类型(可选) constValue: 常量值 返回修改后的源码字节切片和可能的错误 使用示例:
func main() { src := []byte(`package main const existingConst = "existing value" `) newSrc, err := InsertConst(src, "newConst", "int", "42") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertContentAfterFunction ¶ added in v0.1.3
InsertContentAfterFunction 用Go AST解析在指定函数后插入内容 src: 源代码的字节切片 content: 要插入的内容 functionName: 要插入的函数名 返回修改后的源码字节切片和可能的错误 使用示例
func main() { src := []byte(`package main import "fmt" `) newSrc, err := InsertContentAfterFunction(src, "func myfunc() {fmt.Println(\"Hello, World!\")}", "main") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertContentBeforeFunction ¶ added in v0.1.3
InsertContentBeforeFunction 用Go AST解析在指定函数前插入内容 src: 源代码的字节切片 content: 要插入的内容 functionName: 要插入的函数名 返回修改后的源码字节切片和可能的错误 使用示例
func main() { src := []byte(`package main import "fmt" `) newSrc, err := InsertContentBeforeFunction(src, "func myfunc() {fmt.Println(\"Hello, World!\")}", "main") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertFunction ¶ added in v0.1.3
InsertFunction 使用 Go AST 解析在源码中插入一个新的函数 src: 源代码的字节切片 functionName: 要插入的函数名 params: 函数参数列表,格式为 "name type, name type" results: 函数返回值列表,格式为 "type, type" functionBody: 函数体的字符串表示 返回修改后的源码字节切片和可能的错误 使用示例:
func main() { src := []byte(`package main func existingFunction(a int, b string) (int, string) { return a + 1, b + "!" } `) newSrc, err := InsertFunction(src, "newFunction", "a int, b string", "int, string", "return a + 1, b + \"!\"") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertImport ¶ added in v0.1.3
InsertImport 使用 Go AST 解析在源码中插入一个新的Import src: 源代码的字节切片 importPath: 要插入的导入路径 alias: 导入路径的别名(可选) 返回修改后的源码字节切片和可能的错误 使用示例
func main() { src := []byte(`package main import "fmt" `) newSrc, err := InsertImport(src, "io", "") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertStruct ¶ added in v0.1.3
InsertStruct 使用 Go AST 解析在源码中插入一个新的空结构体 src: 源代码的字节切片 structName: 要插入的结构体名称 返回修改后的源码字节切片和可能的错误 使用示例
func main() { src := []byte(`package main type MyStruct struct {} `) newSrc, err := InsertStruct(src, "MyStruct") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func InsertStructField ¶ added in v0.1.3
InsertStructField 使用 Go AST 解析在源码中插入一个新的结构体字段 src: 源代码的字节切片 structName: 结构体名称 fieldName: 要插入的字段名 fieldType: 字段的类型 tag: 字段的标签(可选) 返回修改后的源码字节切片和可能的错误
func InsertStructMethod ¶ added in v0.1.3
func InsertStructMethod(src []byte, structName, methodName, methodBody, receiverName string, usePointerReceiver bool) ([]byte, error)
InsertStructMethod 使用 Go AST 解析在源码中插入一个新的结构体方法 src: 源代码的字节切片 structName: 结构体名称 methodName: 要插入的方法名 methodBody: 方法体的字符串表示 receiverName: 接收者名称(可选) usePointerReceiver: 是否使用指针接收者(可选) 返回修改后的源码字节切片和可能的错误
func InsertVar ¶ added in v0.1.3
InsertVar 使用 Go AST 解析在源码中插入一个新的变量 src: 源代码的字节切片 varName: 变量名称 varType: 变量类型(可选) varValue: 变量值(可选) 返回修改后的源码字节切片和可能的错误
使用示例:
func main() { src := []byte(`package main var existingVar = "existing value" `) newSrc, err := InsertVar(src, "newVar", "int", "42") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func IsConstExists ¶ added in v0.1.3
IsConstExists 使用 Go AST 解析来检查源码中是否存在指定的常量
func IsFunctionExists ¶ added in v0.1.3
IsFunctionExists 使用 Go AST 解析来检查源码中是否存在指定的函数
func IsGoWorkspace ¶ added in v0.1.5
IsGoWorkspace 判断是否是Go工作区
func IsInterfaceExists ¶ added in v0.1.3
IsInterfaceExists 使用 Go AST 解析来检查源码中是否存在指定的接口
func IsStructExists ¶ added in v0.1.3
IsStructExists 使用 Go AST 解析来检查源码中是否存在指定的结构体
func IsStructMethodExists ¶ added in v0.1.3
IsStructMethodExists 使用 Go AST 解析来检查指定结构体类型是否存在指定方法
func IsTypeExists ¶ added in v0.1.3
IsTypeExists 使用 Go AST 解析来检查源码中是否存在指定的类型
func IsVarExists ¶ added in v0.1.3
IsVarExists 使用 Go AST 解析来检查源码中是否存在指定的变量
func NormalizeVendor ¶
NormalizeVendor 将带有 vendor 的包路径转换为正常路径
func ParseGoWork ¶ added in v0.1.5
ParseGoWork 解析go.work文件
func RemoveConst ¶ added in v0.1.3
RemoveConst 使用 Go AST 解析从源码中移除指定的常量 src: 源代码的字节切片 constName: 要移除的常量名称 返回修改后的源码字节切片和可能的错误 使用示例:
func main() { src := []byte(`package main const existingConst = "existing value" `) newSrc, err := RemoveConst(src, "existingConst") if err != nil { log.Fatal(err) } fmt.Println(string(newSrc)) }
func RemoveImport ¶ added in v0.1.3
RemoveImport 使用 Go AST 解析从源码中移除指定的导入 src: 源代码的字节切片 importPath: 要移除的导入路径 返回修改后的源码字节切片和可能的错误
func RemoveStruct ¶ added in v0.1.3
RemoveStruct 使用 Go AST 解析从源码中移除指定的结构体包括方法 src: 源代码的字节切片 structName: 要移除的结构体名称 返回修改后的源码字节切片和可能的错误
func RemoveStructField ¶ added in v0.1.3
RemoveStructField 使用 Go AST 解析从源码中移除指定的结构体字段 src: 源代码的字节切片 structName: 结构体名称 fieldName: 要移除的字段名 返回修改后的源码字节切片和可能的错误
func RemoveUnusedImports ¶
RemoveUnusedImports 从指定的Go文件中移除未使用的包引入
func RemoveUnusedImportsByFile ¶ added in v0.1.3
RemoveUnusedImportsByFile 从指定文件中移除未使用的导入
Types ¶
type GoFmtOption ¶ added in v0.1.5
type GoFmtOption func(opts *goFmtOptions)
func WithGoFmtStderr ¶ added in v0.1.5
func WithGoFmtStderr(stderr io.Writer) GoFmtOption
WithGoFmtStderr 设置错误输出
func WithGoFmtStdout ¶ added in v0.1.5
func WithGoFmtStdout(stdout io.Writer) GoFmtOption
WithGoFmtStdout 设置标准输出
type GoGenerateOption ¶ added in v0.1.5
type GoGenerateOption func(opts *goGenerateOptions)
func WithGoGenerateDir ¶ added in v0.1.5
func WithGoGenerateDir(dir string) GoGenerateOption
WithGoGenerateDir 设置工作目录
func WithGoGenerateEnv ¶ added in v0.1.5
func WithGoGenerateEnv(env []string) GoGenerateOption
WithGoGenerateEnv 设置环境变量
func WithGoGenerateN ¶ added in v0.1.5
func WithGoGenerateN(n bool) GoGenerateOption
WithGoGenerateN 设置是否使用-n选项
func WithGoGenerateRun ¶ added in v0.1.5
func WithGoGenerateRun(run string) GoGenerateOption
WithGoGenerateRun 设置是否使用-run选项
func WithGoGenerateStderr ¶ added in v0.1.5
func WithGoGenerateStderr(stderr io.Writer) GoGenerateOption
WithGoGenerateStderr 设置错误输出
func WithGoGenerateStdout ¶ added in v0.1.5
func WithGoGenerateStdout(stdout io.Writer) GoGenerateOption
WithGoGenerateStdout 设置标准输出
func WithGoGenerateTimeout ¶ added in v0.1.5
func WithGoGenerateTimeout(timeout time.Duration) GoGenerateOption
WithGoGenerateTimeout 设置超时时间
func WithGoGenerateV ¶ added in v0.1.5
func WithGoGenerateV(v bool) GoGenerateOption
WithGoGenerateV 设置是否使用-v选项
func WithGoGenerateX ¶ added in v0.1.5
func WithGoGenerateX(x bool) GoGenerateOption
WithGoGenerateX 设置是否使用-x选项
type GoGetOption ¶ added in v0.1.3
type GoGetOption func(opts *goGetOptions)
func WithGoGetDir ¶ added in v0.1.3
func WithGoGetDir(dir string) GoGetOption
func WithGoGetEnv ¶ added in v0.1.3
func WithGoGetEnv(env []string) GoGetOption
func WithGoGetStderr ¶ added in v0.1.3
func WithGoGetStderr(stderr io.Writer) GoGetOption
func WithGoGetStdout ¶ added in v0.1.3
func WithGoGetStdout(stdout io.Writer) GoGetOption
func WithGoGetUpdateDependencies ¶ added in v0.1.3
func WithGoGetUpdateDependencies(updateDependencies bool) GoGetOption
type GoInstallOption ¶ added in v0.1.3
type GoInstallOption func(opts *goInstallOptions)
func WithGoInstallDir ¶ added in v0.1.3
func WithGoInstallDir(dir string) GoInstallOption
WithGoInstallDir 设置工作目录
func WithGoInstallEnv ¶ added in v0.1.3
func WithGoInstallEnv(env []string) GoInstallOption
WithGoInstallEnv 设置环境变量
func WithGoInstallStderr ¶ added in v0.1.3
func WithGoInstallStderr(stderr io.Writer) GoInstallOption
WithGoInstallStderr 设置错误输出
func WithGoInstallStdout ¶ added in v0.1.3
func WithGoInstallStdout(stdout io.Writer) GoInstallOption
WithGoInstallStdout 设置标准输出
type GoModTidyOption ¶ added in v0.1.3
type GoModTidyOption func(opts *goModTidyOptions)
func WithGoModTidyDir ¶ added in v0.1.3
func WithGoModTidyDir(dir string) GoModTidyOption
WithGoModTidyDir 设置工作目录
func WithGoModTidyEnv ¶ added in v0.1.3
func WithGoModTidyEnv(env []string) GoModTidyOption
WithGoModTidyEnv 设置环境变量
func WithGoModTidyStderr ¶ added in v0.1.3
func WithGoModTidyStderr(stderr io.Writer) GoModTidyOption
WithGoModTidyStderr 设置错误输出
func WithGoModTidyStdout ¶ added in v0.1.3
func WithGoModTidyStdout(stdout io.Writer) GoModTidyOption
WithGoModTidyStdout 设置标准输出
func WithGoModTidyTimeout ¶ added in v0.1.3
func WithGoModTidyTimeout(timeout time.Duration) GoModTidyOption
WithGoModTidyTimeout 设置超时时间
type Imports ¶ added in v0.1.7
type Imports struct {
// contains filtered or unexported fields
}
Imports 管理 Import 实例的集合。
func NewImports ¶ added in v0.1.7
NewImports 创建一个新的 Imports 实例。
type Package ¶ added in v0.1.7
type Package struct {
// contains filtered or unexported fields
}
func NewPackage ¶ added in v0.1.7
func NewPackage() *Package
func (*Package) AddBuildTags ¶ added in v0.1.7
func (*Package) ClearCache ¶ added in v0.1.7
func (p *Package) ClearCache()