Skip to content

Instantly share code, notes, and snippets.

View campoy's full-sized avatar

Francesc Campoy campoy

View GitHub Profile
fs := token.NewFileSet()
for _, arg := range os.Args[1:] {
b, err := ioutil.ReadFile(arg)
if err != nil {
log.Fatal(err)
}
f := fs.AddFile(arg, fs.Base(), len(b))
var s scanner.Scanner
counts := make(map[string]int)
// [code removed for clarity]
for {
_, tok, lit := s.Scan()
if tok == token.EOF {
break
}
if tok == token.IDENT {
package main
import (
"fmt"
"go/parser"
"go/token"
"log"
)
func main() {
package main
import (
"fmt"
"go/parser"
"go/token"
"log"
)
func main() {
package main
import (
"go/parser"
"go/token"
"log"
"github.com/davecgh/go-spew/spew"
)
package main
import (
"go/parser"
"go/printer"
"go/token"
"log"
"os"
)
type Visitor interface {
Visit(node Node) (w Visitor)
}
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
)
type visitor int
func (v visitor) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
fmt.Printf("%s%T\n", strings.Repeat("\t", int(v)), n)
return v + 1
}
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"strings"