AST

What will be printed when the code below is executed?(Hint: Use the template below)

  1. *ast.File:
  2. ____: ____
  3. *ast.GenDecl:
  4. *ast.ValueSpec:
  5. ____: ____
  6. ____: ____
  1. package main
  2. import (
  3. "fmt"
  4. "go/ast"
  5. "go/parser"
  6. "go/token"
  7. "log"
  8. "strings"
  9. )
  10. func main() {
  11. fs := token.NewFileSet()
  12. f, err := parser.ParseFile(fs, "", "package main; var a = 0", parser.AllErrors)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. var v visitor
  17. ast.Walk(v, f)
  18. }
  19. type visitor int
  20. func (v visitor) Visit(n ast.Node) ast.Visitor {
  21. if n == nil {
  22. return nil
  23. }
  24. var s string
  25. switch node := n.(type) {
  26. case *ast.Ident:
  27. s = node.Name
  28. case *ast.BasicLit:
  29. s = node.Value
  30. }
  31. fmt.Printf("%s%T: %s\n", strings.Repeat("\t", int(v)), n, s)
  32. return v + 1
  33. }