Skip to content

Instantly share code, notes, and snippets.

@as
Created August 19, 2018 08:58
Show Gist options
  • Save as/ccdd8ff8507d02dcbe27547ae592fc05 to your computer and use it in GitHub Desktop.
Save as/ccdd8ff8507d02dcbe27547ae592fc05 to your computer and use it in GitHub Desktop.
hello world in go...ast
package main
import (
. "go/ast"
"go/format"
"go/token"
"os"
)
func main() {
format.Node(os.Stdout, token.NewFileSet(), &File{
Name: &Ident{Name: "main"},
Decls: []Decl{
&GenDecl{
Tok: token.IMPORT,
Specs: []Spec{
&ImportSpec{Path: &BasicLit{Kind: token.STRING, Value: `"fmt"`}},
},
},
&FuncDecl{
Name: &Ident{
Name: "main",
},
Type: &FuncType{
Params: &FieldList{},
Results: &FieldList{},
},
Body: &BlockStmt{
List: []Stmt{
&ExprStmt{
X: &CallExpr{
Fun: &SelectorExpr{
X: &Ident{Name: "fmt"},
Sel: &Ident{Name: "Println"},
},
Args: []Expr{
&BasicLit{Kind: token.STRING, Value: `"Hello, playground"`},
},
},
},
},
},
},
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment