Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created September 6, 2018 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astrotars/fb5d7350f2f052d8f50794c010285019 to your computer and use it in GitHub Desktop.
Save astrotars/fb5d7350f2f052d8f50794c010285019 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"go/scanner"
"go/token"
)
func main() {
src := []byte(`package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
`)
var s scanner.Scanner
fset := token.NewFileSet()
file := fset.AddFile("", fset.Base(), len(src))
s.Init(file, src, nil, 0)
for {
pos, tok, lit := s.Scan()
fmt.Printf("%-6s%-8s%q\n", fset.Position(pos), tok, lit)
if tok == token.EOF {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment