How to view the go syntax tree
This article mainly explains "how to view go syntax tree", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to view go syntax tree"!
We know that golang source code after lexical analysis and syntax analysis, you can get the abstract syntax tree ast, users can traverse the abstract syntax tree to achieve source-level modifications. Many of the golang toolsets are based on syntax trees such as go fmt, goimport, go vet, etc.
For those of you who are not familiar with golang or grammar trees, what does a grammar tree contain? What is the structure of the syntax tree? How each part of the source code corresponds to the syntax tree is a place where everyone has more questions. Therefore, the author built a wheel to visually display the syntax tree, and a key to run directly in the browser to open the generated svg format image. Color distinguishes important grammatical structures. and offer profits.
Examples:
For example, we need to analyze the following source code
package main
import ( "fmt" "github.com/xiazemin/golang/ast/ast_graph/exp/multi")
func main() { a:=1 b:=2 fmt.Println(a+b,add(a,b),multi.Multi(a,b),devide(a,b))
}
func devide(i,j int)int { return i/j}
we can do that
package main
import "github.com/xiazemin/golang/ast/ast_graph/gen"
func main() { path := "/Users/didi/goLang/src/github.com/xiazemin/golang/ast/ast_graph/exp/main.go" dpath := "/Users/didi/goLang/src/github.com/xiazemin/golang/ast/ast_graph/" gen.GenSvg(path, dpath, "tree")}
where path is our source path and dpath is the path to generate images
At this point, I believe everyone has a deeper understanding of "how to view the go syntax tree," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!