Golang go/scanner.Scanner.Scan() function examples
package go/scanner
Scan scans the next token and returns the token position, the token, and its literal string if applicable. The source end is indicated by token.EOF.
Golang go/scanner.Scanner.Scan() function usage examples
Example 1:
var s scanner.Scanner
pos, tok, lit = s.Scan()
if tok != token.IDENT || (lit != "true" && lit != "false") {
fmt.Println("expected boolean value")
}
Example 2: (from http://golang.org/pkg/go/scanner/#Scanner.Scan )
package main
import (
"fmt"
"go/scanner"
"go/token"
)
func main() {
// src is the input that we want to tokenize.
src := []byte("cos(x) + 1i*sin(x) // Euler")
// Initialize the scanner.
var s scanner.Scanner
fset := token.NewFileSet() // positions are relative to fset
file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
// Repeated calls to Scan yield the token sequence found in the input.
for {
pos, tok, lit := s.Scan()
if tok == token.EOF {
break
}
fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
}
}
Reference :
Advertisement
Something interesting
Tutorials
+29.2k Golang : missing Git command
+36.5k Golang : Save image to PNG, JPEG or GIF format.
+27.4k Golang : Convert CSV data to JSON format and save to file
+35.1k Golang : Upload and download file to/from AWS S3
+8.8k Golang : Random integer with rand.Seed() within a given range
+62.7k Golang : Convert HTTP Response body to string
+12k Golang : Clean formatting/indenting or pretty print JSON result
+41.4k Golang : Convert string to array/slice
+11.2k CodeIgniter : How to check if a session exist in PHP?
+10.3k Golang : Embed secret text string into binary(executable) file
+5.4k Golang *File points to a file or directory ?
+7.9k Golang : Ways to recover memory during run time.