gocode

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2023 License: MIT Imports: 11 Imported by: 0

README

gocode

使用 go/parser 分析 go 代碼

go/parser 可用於分析 golang 源碼,這對某些時候非常有用,例如識別出包的導出內容然後自動將它們導入到腳本以供腳本調用。

這段代碼是本喵使用 go/parser 進行源碼分析的一些處理代碼

example

example 顯示了如何調用本庫提供的代碼

gocode/gocode

gocode 檔案夾下是一個二進制程序,它可以接收檔案或檔案夾,並分析裏面的golang代碼最終將 golang 中定義的聲明進行打印

./gocode -h
./gocode file main.go
./gocode dir /opt/google/go/src/ -r

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString

func BytesToString(b []byte) string

Convert byte slice to string

It doesn't reallocate memory so it's faster than standard conversion

func IsExport

func IsExport(name string) bool

func ParseDir

func ParseDir(path string, filter func(fs.FileInfo) bool, mode parser.Mode) (pkgs map[string]*Package, e error)

func StringToBytes

func StringToBytes(s string) []byte

Convert string to readonly byte slice

It doesn't reallocate memory so it's faster than standard conversion. But be aware that slices are read-only if written behavior will be unknown.

Types

type Alias

type Alias struct {
	Expr ast.Expr
	Name string
}

func NewAlias

func NewAlias(name string, expr ast.Expr) *Alias

func (*Alias) IsExport

func (a *Alias) IsExport() bool

func (*Alias) String

func (a *Alias) String() string

type ArrayType

type ArrayType struct {
	Type *ast.ArrayType
}

表示了一個切片

func NewArrayType

func NewArrayType(tp *ast.ArrayType) *ArrayType

func (*ArrayType) TypeString

func (t *ArrayType) TypeString() string

type BinaryExpr

type BinaryExpr struct {
	Expr *ast.BinaryExpr
}

表示一個值

func NewBinaryExpr

func NewBinaryExpr(expr *ast.BinaryExpr) *BinaryExpr

func (*BinaryExpr) String

func (b *BinaryExpr) String() string

type CallExpr

type CallExpr struct {
	Expr *ast.CallExpr
}

表示一個函數調用表達式

func NewCallExpr

func NewCallExpr(expr *ast.CallExpr) *CallExpr

func (*CallExpr) String

func (c *CallExpr) String() string

返回類型字符串

type ChanType

type ChanType struct {
	Type *ast.ChanType
}

表示一個chan

func NewChanType

func NewChanType(tp *ast.ChanType) *ChanType

func (*ChanType) TypeString

func (e *ChanType) TypeString() string

返回類型字符串

type Const

type Const struct {
	Specs []ast.Spec
}

func NewConst

func NewConst(specs []ast.Spec) *Const

func (*Const) IsExport

func (c *Const) IsExport() bool

func (*Const) Output

func (c *Const) Output(writer io.Writer, prefix, indent string, all bool) (n int64, e error)

type Field

type Field struct {
	AST  *ast.Field
	Name string
}

func NewField

func NewField(name string, filed *ast.Field) *Field

func (*Field) IsExport

func (f *Field) IsExport() bool

func (*Field) Output

func (f *Field) Output(indent string) string

輸出字段定義

func (*Field) OutputInterface

func (f *Field) OutputInterface() string

輸出接口定義

func (*Field) String

func (f *Field) String() string

type File

type File struct {
	AST *ast.File
	// 檔案名稱
	Name string
	// 導入包
	Imports []*Import
	// 定義的 struct
	Structs []*Struct
	// 定義的 interface
	Interfaces []*Interface
	// 定義的別名 type xxx xxx
	Alias []*Alias
	// 定義的常量
	Consts []*Const
	// 定義的變量
	Vars []*Var
	// 定義的函數
	Funcs []*Func
}

包中的檔案信息

func NewFile

func NewFile(name string, f *ast.File) *File

func ParseFile

func ParseFile(path string, src any, mode parser.Mode) (file *File, e error)

func (*File) Output

func (f *File) Output(writer io.Writer, prefix, indent string, all bool) (n int64, e error)

func (*File) String

func (f *File) String() string

type Func

type Func struct {
	Expr *ast.FuncDecl
}

func NewFunc

func NewFunc(expr *ast.FuncDecl) *Func

func (*Func) IsExport

func (f *Func) IsExport() bool

func (*Func) String

func (f *Func) String() string

type FuncType

type FuncType struct {
	Type *ast.FuncType
}

表示了一個函數

func NewFuncType

func NewFuncType(tp *ast.FuncType) *FuncType

func (*FuncType) ParamsAndResults

func (t *FuncType) ParamsAndResults() string

返回參數和返回值定義

func (*FuncType) TypeString

func (t *FuncType) TypeString() string

type Ident

type Ident struct {
	Ident *ast.Ident
}

表示了一個元素的數據類型

func NewIdent

func NewIdent(ident *ast.Ident) *Ident

func (*Ident) TypeString

func (t *Ident) TypeString() string

type Import

type Import struct {
	AST *ast.ImportSpec
}

func NewImport

func NewImport(imt *ast.ImportSpec) *Import

func (*Import) Name

func (imt *Import) Name() string

返回 import 名稱

func (*Import) Path

func (imt *Import) Path() string

返回 import 路徑

func (*Import) String

func (imt *Import) String() string

type Interface

type Interface struct {
	AST  *ast.InterfaceType
	Name string
}

func NewInterface

func NewInterface(name string, it *ast.InterfaceType) *Interface

func (*Interface) IsExport

func (it *Interface) IsExport() bool

func (*Interface) IsTemplate

func (it *Interface) IsTemplate() bool

func (*Interface) Output

func (it *Interface) Output(writer io.Writer, prefix, indent string) (n int64, e error)

func (*Interface) String

func (it *Interface) String() string

type MapType

type MapType struct {
	Type *ast.MapType
}

表示了一個hash表

func NewMapType

func NewMapType(tp *ast.MapType) *MapType

func (*MapType) TypeString

func (t *MapType) TypeString() string

type Package

type Package struct {
	AST *ast.Package
	// 檔案
	Files []*File
}

包信息

func NewPackage

func NewPackage(p *ast.Package) *Package

func (*Package) Name

func (p *Package) Name() string

返回包名

func (*Package) Output

func (p *Package) Output(writer io.Writer, prefix, indent string, all bool) (n int64, e error)

將人類友好字符串寫入到 writer

func (*Package) String

func (p *Package) String() string

返回人類友好的字符串

type SelectorExpr

type SelectorExpr struct {
	Expr *ast.SelectorExpr
}

表示一個選擇表達式

func NewSelectorExpr

func NewSelectorExpr(expr *ast.SelectorExpr) *SelectorExpr

func (*SelectorExpr) TypeString

func (e *SelectorExpr) TypeString() string

返回類型字符串

type StarExpr

type StarExpr struct {
	Expr *ast.StarExpr
}

表示一個指針表達式

func NewStarExpr

func NewStarExpr(expr *ast.StarExpr) *StarExpr

func (*StarExpr) TypeString

func (e *StarExpr) TypeString() string

返回類型字符串

type Struct

type Struct struct {
	AST    *ast.StructType
	Name   string
	Fields []*Field
}

func NewStruct

func NewStruct(name string, st *ast.StructType) *Struct

func (*Struct) IsExport

func (s *Struct) IsExport() bool

func (*Struct) Output

func (s *Struct) Output(writer io.Writer, prefix, indent string, all bool) (n int64, e error)

func (*Struct) String

func (s *Struct) String() string

type TypeExpr

type TypeExpr struct {
	Expr ast.Expr
}

表示一個型別

func NewTypeExpr

func NewTypeExpr(expr ast.Expr) *TypeExpr

func (*TypeExpr) TypeString

func (e *TypeExpr) TypeString() string

返回類型字符串

type ValueExpr

type ValueExpr struct {
	Expr ast.Expr
}

表示一個值

func NewValueExpr

func NewValueExpr(expr ast.Expr) *ValueExpr

func (*ValueExpr) String

func (v *ValueExpr) String() string

type ValueSpec

type ValueSpec struct {
	Spec *ast.ValueSpec
}

func NewValueSpec

func NewValueSpec(spec *ast.ValueSpec) *ValueSpec

func (*ValueSpec) IsExport

func (v *ValueSpec) IsExport() bool

func (*ValueSpec) Output

func (v *ValueSpec) Output(all bool) (s string, e error)

type Var

type Var struct {
	Specs []ast.Spec
}

func NewVar

func NewVar(specs []ast.Spec) *Var

func (*Var) IsExport

func (v *Var) IsExport() bool

func (*Var) Output

func (v *Var) Output(writer io.Writer, prefix, indent string, all bool) (n int64, e error)

Jump to

Keyboard shortcuts

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