前言
bufio模塊是golang標(biāo)準(zhǔn)庫(kù)中的模塊之一,主要是實(shí)現(xiàn)了一個(gè)讀寫(xiě)的緩存,用于對(duì)數(shù)據(jù)的讀取或者寫(xiě)入操作。該模塊在多個(gè)涉及io的標(biāo)準(zhǔn)庫(kù)中被使用,比如http模塊中使用buffio來(lái)完成網(wǎng)絡(luò)數(shù)據(jù)的讀寫(xiě),壓縮文件的zip模塊利用bufio來(lái)操作文件數(shù)據(jù)的讀寫(xiě)等。
golang的bufio包里面定以的SplitFunc是一個(gè)比較重要也比較難以理解的東西,本文希望通過(guò)結(jié)合簡(jiǎn)單的實(shí)例介紹SplitFunc的工作原理以及如何實(shí)現(xiàn)一個(gè)自己的SplitFunc。
一個(gè)例子
在bufio包里面定義了一些常用的工具比如Scanner,你可能需要讀取用戶在標(biāo)準(zhǔn)輸入里面輸入的一些東西,比如我們做一個(gè)復(fù)讀機(jī),讀取用戶的每一行輸入,然后打印出來(lái):
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
這個(gè)程序很簡(jiǎn)單,os.Stdin實(shí)現(xiàn)了io.Reader接口,我們從這個(gè)reader創(chuàng)建了一個(gè)scanner,設(shè)置分割函數(shù)為bufio.ScanLines,然后for循環(huán),每次讀到一行數(shù)據(jù)就將文本內(nèi)容打印出來(lái)。麻雀雖小五臟俱全,這個(gè)小程序雖然簡(jiǎn)單,卻引出了我們今天要介紹的對(duì)象: bufio.SplitFunc,它的定義是這個(gè)樣子的:
package "buffio"
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)
golang官方文檔的描述是這個(gè)樣子的:
SplitFunc is the signature of the split function used to tokenize the input. The arguments are an initial substring of the remaining unprocessed data and a flag, atEOF, that reports whether the Reader has no more data to give. The return values are the number of bytes to advance the input and the next token to return to the user, if any, plus an error, if any.
Scanning stops if the function returns an error, in which case some of the input may be discarded.
Otherwise, the Scanner advances the input. If the token is not nil, the Scanner returns it to the user. If the token is nil, the Scanner reads more data and continues scanning; if there is no more data--if atEOF was true--the Scanner returns. If the data does not yet hold a complete token, for instance if it has no newline while scanning lines, a SplitFunc can return (0, nil, nil) to signal the Scanner to read more data into the slice and try again with a longer slice starting at the same point in the input.
The function is never called with an empty data slice unless atEOF is true. If atEOF is true, however, data may be non-empty and, as always, holds unprocessed text.
英文!參數(shù)這么多!返回值這么多!好煩!不知道各位讀者遇到這種文檔會(huì)不會(huì)有這種感覺(jué)...正式由于這種情況,我才決定寫(xiě)一篇文章介紹一下SplitFunc的具體工作原理,用一種通俗的方式結(jié)合具體實(shí)例加以說(shuō)明,希望對(duì)讀者有所幫助。
好了,廢話少說(shuō),開(kāi)始正題吧!
Scanner和SplitFunc的工作機(jī)制
package "buffio"
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)
Scanner是有緩存的,意思是Scanner底層維護(hù)了一個(gè)Slice用來(lái)保存已經(jīng)從Reader中讀取的數(shù)據(jù),Scanner會(huì)調(diào)用我們?cè)O(shè)置SplitFunc,將緩沖區(qū)內(nèi)容(data)和是否已經(jīng)輸入完了(atEOF)以參數(shù)的形式傳遞給SplitFunc,而SplitFunc的職責(zé)就是根據(jù)上述的兩個(gè)參數(shù)返回下一次Scan需要前進(jìn)幾個(gè)字節(jié)(advance),分割出來(lái)的數(shù)據(jù)(token),以及錯(cuò)誤(err)。
這是一個(gè)通信雙向的過(guò)程,Scanner告訴我們的SplitFunc已經(jīng)掃描到的數(shù)據(jù)和是否到結(jié)尾了,我們的SplitFunc則根據(jù)這些信息將分割的結(jié)果返回和下次掃描需要前進(jìn)的位置返回給Scanner。用一個(gè)例子來(lái)說(shuō)明:
package main
import (
"bufio"
"fmt"
"strings"
)
func main() {
input := "abcdefghijkl"
scanner := bufio.NewScanner(strings.NewReader(input))
split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
fmt.Printf("%t\t%d\t%s\n", atEOF, len(data), data)
return 0, nil, nil
}
scanner.Split(split)
buf := make([]byte, 2)
scanner.Buffer(buf, bufio.MaxScanTokenSize)
for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
}
}
輸出
false 2 ab
false 4 abcd
false 8 abcdefgh
false 12 abcdefghijkl
true 12 abcdefghijkl
這里我們把緩沖區(qū)的初始大小設(shè)置為了2,不夠的時(shí)候會(huì)擴(kuò)展為原來(lái)的2倍,最大為bufio.MaxScanTokenSize,這樣一開(kāi)始掃描2個(gè)字節(jié),我們的緩沖區(qū)就滿了,reader的內(nèi)容還沒(méi)有讀取到EOF,然后split函數(shù)執(zhí)行,輸出:
false 2 ab
緊接著函數(shù)返回 0, nil, nil這個(gè)返回值告訴Scanner數(shù)據(jù)不夠,下次讀取的位置前進(jìn)0位,需要繼續(xù)從reader里面讀取,此時(shí)因?yàn)榫彌_區(qū)滿了,所以容量擴(kuò)展為2 * 2 = 4,reader的內(nèi)容還沒(méi)有讀取到EOF,輸出
false 4 abcd
重復(fù)上述步驟,一直到最后全部?jī)?nèi)容讀取完了,EOF此時(shí)變成了true
true 12 abcdefghijkl
看了上面的過(guò)程是不是對(duì)SplitFunc的工作原來(lái)有了一點(diǎn)理解了呢?再回頭看一下golang的官方文檔有沒(méi)有覺(jué)得稍微理解了一點(diǎn)?下面是bufio.ScanLines的實(shí)現(xiàn),讀者可以自己研究一下該函數(shù)是如何工作的
標(biāo)準(zhǔn)庫(kù)里的ScanLines
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
// 表示我們已經(jīng)掃描到結(jié)尾了
if atEOF len(data) == 0 {
return 0, nil, nil
}
// 找到\n的位置
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// 把下次開(kāi)始讀取的位置向前移動(dòng)i + 1位
return i + 1, dropCR(data[0:i]), nil
}
// 這里處理的reader內(nèi)容全部讀取完了,但是內(nèi)容不為空,所以需要把剩余的數(shù)據(jù)返回
if atEOF {
return len(data), dropCR(data), nil
}
// 表示現(xiàn)在不能分割,向Reader請(qǐng)求更多的數(shù)據(jù)
return 0, nil, nil
}
參考
In-depth introduction to bufio.Scanner in Golang
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
您可能感興趣的文章:- 淺談golang類(lèi)型斷言,失敗類(lèi)型斷言返回值問(wèn)題
- 通過(guò)匯編看golang函數(shù)的多返回值問(wèn)題
- Golang的func參數(shù)及返回值操作