我正在尝试从go AST提取函数调用
$ cat main.go
package main
import ("fmt"; "go/ast"; "go/token"; "go/parser"; "io/ioutil")
func CalledFuncs(n ast.Node) bool {
switch n.(type) {
case *ast.CallExpr:
l := ast.CallExpr(n).Lparen
r := ast.CallExpr(n).Right
fmt.Printf("call %d( ... %d)\n",l,r)
}
return true
}
func main() {
fset := token.NewFileSet()
src,_ := ioutil.ReadFile("simple.go")
f, _ := parser.ParseFile(fset,">>",src,0)
ast.Inspect(f, CalledFuncs)
}但是go编译器抱怨说我不能进行转换:
$ go build -o main main.go
# command-line-arguments
./main.go:7:26: cannot convert n (type ast.Node) to type ast.CallExpr
./main.go:8:26: cannot convert n (type ast.Node) to type ast.CallExpr我可能漏掉了什么。
发布于 2021-08-01 14:27:23
https://stackoverflow.com/questions/68611125
复制相似问题