Created
May 25, 2020 21:58
-
-
Save theunrepentantgeek/c1cfbd8044bf273da61660d01995545c to your computer and use it in GitHub Desktop.
Dump a Go source file as an Abstract Syntax Tree (AST)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"go/ast" | |
"go/format" | |
"go/parser" | |
"go/token" | |
//"github.com/davecgh/go-spew/spew" | |
) | |
func main() { | |
// positions are relative to fset | |
fset := token.NewFileSet() | |
// Read in the original file | |
snippet, err := parser.ParseFile(fset, "original.go", nil, parser.Trace | parser.ParseComments) | |
// Print structure to stdout | |
ast.Print(fset, snippet) | |
// Write to a final file for comparison | |
f, err := os.Create("demo.txt") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(-1) | |
} | |
defer f.Close() | |
err = format.Node(f, fset, snippet) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(-1) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Microsoft Corporation. | |
// Licensed under the MIT license. | |
// This is a generated file. Please do not make manual changes. | |
package main | |
func DemoFunc() (this *int, that *int) { | |
return nil, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment