Created
September 6, 2018 17:42
-
-
Save astrotars/fb5d7350f2f052d8f50794c010285019 to your computer and use it in GitHub Desktop.
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" | |
"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