Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "go/scanner"
  6. "go/token"
  7. "io/ioutil"
  8. )
  9.  
  10. func main() {
  11. // src is the input that we want to tokenize.
  12. src, _ := ioutil.ReadFile(`main.go`)
  13.  
  14. // Initialize the scanner.
  15. var s scanner.Scanner
  16. fset := token.NewFileSet() // positions are relative to fset
  17. file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
  18. s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
  19.  
  20. // Repeated calls to Scan yield the token sequence found in the input.
  21. for {
  22. pos, tok, lit := s.Scan()
  23. if tok == token.EOF {
  24. break
  25. }
  26. fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement