Standard library / go/scanner

go/scanner.Scanner.Scan()

Reads the next token with compact position and literal. Check ErrorCount even if no ILLEGAL token occurs. Expected stdout: 'IDENT "x"\n:= ""\nINT "1"\n; "\\n"\n'.

Formal Syntax & Structure

go syntaxgo
func (s *Scanner) Scan() (pos token.Pos, tok token.Token, lit string)

Return Value

(pos token.Pos, tok token.Token, lit string)

Reads the next token with compact position and literal.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/scanner"  "go/token")
func main() {  src := []byte("x := 1")  f := token.NewFileSet().AddFile("demo.go", -1, len(src))  var s scanner.Scanner  s.Init(f, src, nil, 0)  for {    _, t, l := s.Scan()    if t == token.EOF {      break    }    fmt.Printf("%s %q\n", t, l)  }}
Go Reference