Go ast is very useful mechanism which is provide how to read your text code into valid go code without checking scope and undefined variables.
But normal AST in go language are not provide all information. Such as data type of binary expression, calling method return type and package of calling method etc.
Here provides basic example which is mention some missing information.
// Go Code
package main
import (
"fmt"
"math"
"math/rand"
)
func main() {
fmt.Println(math.Abs(-3))
fmt.Println(rand.Intn(3))
}
AST Output
{ "source": [ { "0": "ast.File", "Package": "1:1", "Name": { "0": "ast.Ident", "NamePos": "1:9", "Name": "main" }, "Decls": [ { "0": "ast.GenDecl", "TokPos": "3:1", "Tok": "import", "Lparen": "3:8", "Specs": [ { "0": "ast.ImportSpec", "Path": { "0": "ast.BasicLit", "ValuePos": "4:2", "Kind": "STRING", "Value": "\"fmt\"" }, "EndPos": "-" }, { "0": "ast.ImportSpec", "Path": { "0": "ast.BasicLit", "ValuePos": "5:2", "Kind": "STRING", "Value": "\"math\"" }, "EndPos": "-" }, { "0": "ast.ImportSpec", "Path": { "0": "ast.BasicLit", "ValuePos": "6:2", "Kind": "STRING", "Value": "\"math/rand\"" }, "EndPos": "-" } ], "Rparen": "7:1" }, { "0": "ast.FuncDecl", "Name": { "0": "ast.Ident", "NamePos": "9:6", "is": "Func", "is_signature": "func main()", "is_pkg": "main", "is_path": "", "Name": "main", "Obj": { "0": "ast.Object", "Kind": "func", "Name": "main", "Decl": "39" } }, "Type": { "0": "ast.FuncType", "Func": "9:1", "Params": { "0": "ast.FieldList", "Opening": "9:10", "Closing": "9:11" } }, "Body": { "0": "ast.BlockStmt", "Lbrace": "9:13", "List": [ { "0": "ast.ExprStmt", "X": { "0": "ast.CallExpr", "Fun": { "0": "ast.SelectorExpr", "X": { "0": "ast.Ident", "NamePos": "10:2", "is": "PkgName", "is_pakage": "main", "is_imported": "fmt", "Name": "fmt" }, "Sel": { "0": "ast.Ident", "NamePos": "10:6", "is_path": "fmt", "is": "Func", "is_signature": "func fmt.Println(a ...any) (n int, err error)", "is_pkg": "fmt", "Name": "Println" } }, "Lparen": "10:13", "return": "(n int, err error)", "Args": [ { "0": "ast.CallExpr", "Fun": { "0": "ast.SelectorExpr", "X": { "0": "ast.Ident", "NamePos": "10:14", "is": "PkgName", "is_pakage": "main", "is_imported": "math", "Name": "math" }, "Sel": { "0": "ast.Ident", "NamePos": "10:19", "is": "Func", "is_signature": "func math.Abs(x float64) float64", "is_pkg": "math", "is_path": "math", "Name": "Abs" } }, "Lparen": "10:22", "return": "float64", "Args": [ { "0": "ast.UnaryExpr", "OpPos": "10:23", "Op": "-", "X": { "0": "ast.BasicLit", "ValuePos": "10:24", "Kind": "INT", "Value": "3" } } ], "Ellipsis": "-", "Rparen": "10:25" } ], "Ellipsis": "-", "Rparen": "10:26" } }, { "0": "ast.ExprStmt", "X": { "0": "ast.CallExpr", "Fun": { "0": "ast.SelectorExpr", "X": { "0": "ast.Ident", "NamePos": "11:2", "is_imported": "fmt", "is": "PkgName", "is_pakage": "main", "Name": "fmt" }, "Sel": { "0": "ast.Ident", "NamePos": "11:6", "is_signature": "func fmt.Println(a ...any) (n int, err error)", "is_pkg": "fmt", "is_path": "fmt", "is": "Func", "Name": "Println" } }, "Lparen": "11:13", "return": "(n int, err error)", "Args": [ { "0": "ast.CallExpr", "Fun": { "0": "ast.SelectorExpr", "X": { "0": "ast.Ident", "NamePos": "11:14", "is": "PkgName", "is_pakage": "main", "is_imported": "rand", "Name": "rand" }, "Sel": { "0": "ast.Ident", "NamePos": "11:19", "is_pkg": "rand", "is_path": "math/rand", "is": "Func", "is_signature": "func math/rand.Intn(n int) int", "Name": "Intn" } }, "Lparen": "11:23", "return": "int", "Args": [ { "0": "ast.BasicLit", "ValuePos": "11:24", "Kind": "INT", "Value": "3" } ], "Ellipsis": "-", "Rparen": "11:25" } ], "Ellipsis": "-", "Rparen": "11:26" } } ], "Rbrace": "12:1" } } ], "Scope": { "0": "ast.Scope", "Objects": { "0": "map[string]*ast.Object", "main": "47" } }, "Imports": [ "12", "20", "28" ], "Unresolved": [ "66", "87", "127", "148" ] } ] }
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment