github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/text/scanner/example_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package scanner_test
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"text/scanner"
    11  )
    12  
    13  func Example() {
    14  	const src = `
    15  // This is scanned code.
    16  if a > 10 {
    17  	someParsable = text
    18  }`
    19  	var s scanner.Scanner
    20  	s.Init(strings.NewReader(src))
    21  	s.Filename = "example"
    22  	for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
    23  		fmt.Printf("%s: %s\n", s.Position, s.TokenText())
    24  	}
    25  
    26  	// Output:
    27  	// example:3:1: if
    28  	// example:3:4: a
    29  	// example:3:6: >
    30  	// example:3:8: 10
    31  	// example:3:11: {
    32  	// example:4:2: someParsable
    33  	// example:4:15: =
    34  	// example:4:17: text
    35  	// example:5:1: }
    36  }