204 lines
4.0 KiB
Go
204 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
// 设置文件路径
|
|
filePath := "/home/eson/workspace/requests"
|
|
|
|
// 解析指定目录下的所有Go源代码文件
|
|
fset := token.NewFileSet()
|
|
pkgs, err := parser.ParseDir(fset, filePath, nil, 0)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
// 遍历每个包
|
|
for _, pkg := range pkgs {
|
|
// 遍历每个文件
|
|
for _, file := range pkg.Files {
|
|
// 遍历每个声明
|
|
for _, decl := range file.Decls {
|
|
// 检查声明是否为函数声明
|
|
if decl, ok := decl.(*ast.FuncDecl); ok {
|
|
// 检查函数是否导出(暴露)
|
|
if decl.Recv == nil && decl.Name.IsExported() {
|
|
// 打印函数签名
|
|
|
|
fmt.Printf("%s(%#v, %#v) error\n", decl.Name, decl.Type.Params, decl.Type.Results)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 辅助函数:将类型转换为字符串表示
|
|
func typeString(typ ast.Expr) string {
|
|
return os.Getenv("GOPATH") + "/src/reflect/type.go" // 这里只是一个示例,你需要替换为你的实际代码来解析类型并将其转换为字符串表示
|
|
}
|
|
|
|
func main2() {
|
|
fset := token.NewFileSet()
|
|
pkgs, err := parser.ParseDir(fset, "/home/eson/workspace/requests", nil, 0)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
for _, pkg := range pkgs {
|
|
for fname, f := range pkg.Files {
|
|
fmt.Println("File:", fname)
|
|
ast.Inspect(f, func(n ast.Node) bool {
|
|
switch x := n.(type) {
|
|
case *ast.FuncDecl:
|
|
fmt.Printf(" Func: %v\n", x.Name.Name)
|
|
fmt.Printf(" Receiver: ")
|
|
if x.Recv != nil {
|
|
for _, l := range x.Recv.List {
|
|
fmt.Printf("%v ", l.Type)
|
|
}
|
|
}
|
|
fmt.Println()
|
|
fmt.Printf(" Params:")
|
|
if x.Type.Params != nil {
|
|
for _, f := range x.Type.Params.List {
|
|
for _, p := range f.Names {
|
|
fmt.Printf(" %s (%s)", p.Name, inspectExpr(f.Type))
|
|
}
|
|
}
|
|
}
|
|
|
|
fmt.Println()
|
|
fmt.Printf(" Results:")
|
|
if x.Type.Results != nil {
|
|
for _, f := range x.Type.Results.List {
|
|
fmt.Printf(" %v", f.Type)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func inspectExpr(n ast.Expr) string {
|
|
var s string
|
|
|
|
switch x := n.(type) {
|
|
|
|
case *ast.BadExpr:
|
|
s = fmt.Sprintf("From %v To %v", x.From, x.To)
|
|
|
|
case *ast.Ident:
|
|
s = x.Name
|
|
|
|
case *ast.Ellipsis:
|
|
s = inspectExpr(x.Elt)
|
|
|
|
case *ast.BasicLit:
|
|
s = x.Value
|
|
|
|
case *ast.FuncLit:
|
|
s = fmt.Sprintf("%#v", x)
|
|
|
|
case *ast.CompositeLit:
|
|
s = fmt.Sprintf("%#v", x)
|
|
for _, elt := range x.Elts {
|
|
s += inspectExpr(elt)
|
|
}
|
|
|
|
case *ast.ParenExpr:
|
|
s = inspectExpr(x.X)
|
|
|
|
case *ast.SelectorExpr:
|
|
s = fmt.Sprintf("%s.%s", inspectExpr(x.X), x.Sel.Name)
|
|
|
|
case *ast.IndexExpr:
|
|
s = inspectExpr(x.X)
|
|
|
|
case *ast.IndexListExpr:
|
|
s = inspectExpr(x.X)
|
|
for _, index := range x.Indices {
|
|
s += inspectExpr(index)
|
|
}
|
|
|
|
case *ast.SliceExpr:
|
|
s = inspectExpr(x.X)
|
|
|
|
case *ast.TypeAssertExpr:
|
|
s = inspectExpr(x.X)
|
|
|
|
case *ast.CallExpr:
|
|
s = inspectExpr(x.Fun)
|
|
for _, arg := range x.Args {
|
|
s += inspectExpr(arg)
|
|
}
|
|
|
|
case *ast.StarExpr:
|
|
s = inspectExpr(x.X)
|
|
|
|
case *ast.UnaryExpr:
|
|
s = inspectExpr(x.X)
|
|
|
|
case *ast.BinaryExpr:
|
|
s = inspectExpr(x.X)
|
|
s += inspectExpr(x.Y)
|
|
|
|
case *ast.KeyValueExpr:
|
|
s = fmt.Sprintf("%#v", inspectExpr(x.Key))
|
|
s += inspectExpr(x.Value)
|
|
|
|
case *ast.ArrayType:
|
|
s = fmt.Sprintf("%#v", x)
|
|
s += inspectExpr(x.Elt)
|
|
|
|
case *ast.StructType:
|
|
s = fmt.Sprintf("%#v", x)
|
|
|
|
case *ast.FuncType:
|
|
s = fmt.Sprintf("%#v", x)
|
|
if x.Params != nil {
|
|
for _, field := range x.Params.List {
|
|
s += inspectExpr(field.Type)
|
|
}
|
|
}
|
|
if x.Results != nil {
|
|
for _, field := range x.Results.List {
|
|
s += inspectExpr(field.Type)
|
|
}
|
|
}
|
|
|
|
case *ast.InterfaceType:
|
|
s = fmt.Sprintf("%#v", x)
|
|
for _, field := range x.Methods.List {
|
|
s += inspectExpr(field.Type)
|
|
}
|
|
|
|
case *ast.MapType:
|
|
s = fmt.Sprintf("%#v", x)
|
|
s += inspectExpr(x.Key)
|
|
s += inspectExpr(x.Value)
|
|
|
|
case *ast.ChanType:
|
|
s = fmt.Sprintf("%#v", x)
|
|
s += inspectExpr(x.Value)
|
|
|
|
default:
|
|
s = fmt.Sprintf("%T", n)
|
|
|
|
}
|
|
|
|
return s
|
|
}
|