This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
text/scanner: add runnable example for package
Change-Id: I42a952b04a56fb888fa7d5d9c2b56cbdd3434034 Reviewed-on: https://go-review.googlesource.com/11246 Reviewed-by: Andrew Gerrand <[email protected]>
- Loading branch information
Showing
2 changed files
with
38 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2015 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package scanner_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"text/scanner" | ||
) | ||
|
||
func Example() { | ||
const src = ` | ||
// This is scanned code. | ||
if a > 10 { | ||
someParsable = text | ||
}` | ||
var s scanner.Scanner | ||
s.Init(strings.NewReader(src)) | ||
var tok rune | ||
for tok != scanner.EOF { | ||
tok = s.Scan() | ||
fmt.Println("At position", s.Pos(), ":", s.TokenText()) | ||
} | ||
|
||
// Output: | ||
// At position 3:4 : if | ||
// At position 3:6 : a | ||
// At position 3:8 : > | ||
// At position 3:11 : 10 | ||
// At position 3:13 : { | ||
// At position 4:15 : someParsable | ||
// At position 4:17 : = | ||
// At position 4:22 : text | ||
// At position 5:3 : } | ||
// At position 5:3 : | ||
} |
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