forked from msoap/go-carpet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
38 lines (32 loc) · 756 Bytes
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"go/ast"
"go/parser"
"go/token"
)
// Func - one go function in source
type Func struct {
Name string
Begin, End int
}
// getGolangFuncs - parse golang source file and get all functions
// funcs, err := getGolangFuncs(goFileContentInBytes)
func getGolangFuncs(fileContent []byte) (result []Func, err error) {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, "", fileContent, 0)
if err != nil {
return result, err
}
ast.Inspect(astFile, func(nodeRaw ast.Node) bool {
switch node := nodeRaw.(type) {
case *ast.FuncDecl:
result = append(result, Func{
Name: node.Name.String(),
Begin: int(node.Pos()),
End: int(node.End()),
})
}
return true
})
return result, nil
}