當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Scanner用法及代碼示例

GO語言"bufio"包中"Scanner"類型的用法及代碼示例。

Scanner 提供了一個方便的接口來讀取數據,例如 newline-delimited 行文本的文件。對 Scan 方法的連續調用將逐步遍曆文件的'tokens',跳過標記之間的字節。令牌的規範由 SplitFunc 類型的拆分函數定義;默認的 split 函數將輸入拆分為行終止剝離的行。此包中定義了拆分函數,用於將文件掃描為行、字節、UTF-8 編碼的符文和空格分隔的單詞。客戶端可以改為提供自定義拆分函數。

掃描在 EOF、第一個 I/O 錯誤或太大而無法放入緩衝區的標記處不可恢複地停止。當掃描停止時,閱讀器可能已經任意前進到最後一個標記之外。需要對錯誤處理或大令牌進行更多控製,或者必須在閱讀器上運行順序掃描的程序應該使用 bufio.Reader 代替。

用法:

type Scanner struct {
    // contains filtered or unexported fields
}

示例(自定義):

使用具有自定義拆分函數(通過包裝 ScanWords 構建)的掃描儀來驗證 32 位十進製輸入。

package main

import (
    "bufio"
    "fmt"
    "strconv"
    "strings"
)

func main() {
    // An artificial input source.
    const input = "1234 5678 1234567901234567890"
    scanner := bufio.NewScanner(strings.NewReader(input))
    // Create a custom split function by wrapping the existing ScanWords function.
    split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
        advance, token, err = bufio.ScanWords(data, atEOF)
        if err == nil && token != nil {
            _, err = strconv.ParseInt(string(token), 10, 32)
        }
        return
    }
    // Set the split function for the scanning operation.
    scanner.Split(split)
    // Validate the input
    for scanner.Scan() {
        fmt.Printf("%s\n", scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        fmt.Printf("Invalid input: %s", err)
    }
}

輸出:

1234
5678
Invalid input: strconv.ParseInt: parsing "1234567901234567890": value out of range

示例(EmptyFinalToken):

使用帶有自定義拆分函數的掃描器來解析一個帶有空最終值的逗號分隔列表。

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    // Comma-separated list; last entry is empty.
    const input = "1,2,3,4,"
    scanner := bufio.NewScanner(strings.NewReader(input))
    // Define a split function that separates on commas.
    onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
        for i := 0; i < len(data); i++ {
            if data[i] == ',' {
                return i + 1, data[:i], nil
            }
        }
        if !atEOF {
            return 0, nil, nil
        }
        // There is one final token to be delivered, which may be the empty string.
        // Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this
        // but does not trigger an error to be returned from Scan itself.
        return 0, data, bufio.ErrFinalToken
    }
    scanner.Split(onComma)
    // Scan.
    for scanner.Scan() {
        fmt.Printf("%q ", scanner.Text())
    }
    if err := scanner.Err(); err != nil {
        fmt.Fprintln(os.Stderr, "reading input:", err)
    }
}

輸出:

"1" "2" "3" "4" ""

示例(行):

Scanner 的最簡單用法,將標準輸入讀取為一組行。

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        fmt.Println(scanner.Text()) // Println will add back the final '\n'
    }
    if err := scanner.Err(); err != nil {
        fmt.Fprintln(os.Stderr, "reading standard input:", err)
    }
}

示例(單詞):

使用掃描器通過將輸入掃描為以空格分隔的標記序列來實現簡單的word-count 實用程序。

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    // An artificial input source.
    const input = "Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n"
    scanner := bufio.NewScanner(strings.NewReader(input))
    // Set the split function for the scanning operation.
    scanner.Split(bufio.ScanWords)
    // Count the words.
    count := 0
    for scanner.Scan() {
        count++
    }
    if err := scanner.Err(); err != nil {
        fmt.Fprintln(os.Stderr, "reading input:", err)
    }
    fmt.Printf("%d\n", count)
}

輸出:

15

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Scanner。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。