校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > golang中bufio.SplitFunc的深入理解

golang中bufio.SplitFunc的深入理解

熱門標(biāo)簽:浙江高速公路地圖標(biāo)注 學(xué)海導(dǎo)航地圖標(biāo)注 廣州呼叫中心外呼系統(tǒng) 江西轉(zhuǎn)化率高的羿智云外呼系統(tǒng) 高德地圖標(biāo)注口訣 西部云谷一期地圖標(biāo)注 南通如皋申請開通400電話 地圖標(biāo)注的汽車標(biāo) 中國地圖標(biāo)注省會高清

前言

bufio模塊是golang標(biāo)準(zhǔn)庫中的模塊之一,主要是實(shí)現(xiàn)了一個(gè)讀寫的緩存,用于對數(shù)據(jù)的讀取或者寫入操作。該模塊在多個(gè)涉及io的標(biāo)準(zhǔn)庫中被使用,比如http模塊中使用buffio來完成網(wǎng)絡(luò)數(shù)據(jù)的讀寫,壓縮文件的zip模塊利用bufio來操作文件數(shù)據(jù)的讀寫等。

golang的bufio包里面定以的SplitFunc是一個(gè)比較重要也比較難以理解的東西,本文希望通過結(jié)合簡單的實(shí)例介紹SplitFunc的工作原理以及如何實(shí)現(xiàn)一個(gè)自己的SplitFunc。

一個(gè)例子

在bufio包里面定義了一些常用的工具比如Scanner,你可能需要讀取用戶在標(biāo)準(zhǔn)輸入里面輸入的一些東西,比如我們做一個(gè)復(fù)讀機(jī),讀取用戶的每一行輸入,然后打印出來:

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è)程序很簡單,os.Stdin實(shí)現(xiàn)了io.Reader接口,我們從這個(gè)reader創(chuàng)建了一個(gè)scanner,設(shè)置分割函數(shù)為bufio.ScanLines,然后for循環(huán),每次讀到一行數(shù)據(jù)就將文本內(nèi)容打印出來。麻雀雖小五臟俱全,這個(gè)小程序雖然簡單,卻引出了我們今天要介紹的對象: 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ù)這么多!返回值這么多!好煩!不知道各位讀者遇到這種文檔會不會有這種感覺...正式由于這種情況,我才決定寫一篇文章介紹一下SplitFunc的具體工作原理,用一種通俗的方式結(jié)合具體實(shí)例加以說明,希望對讀者有所幫助。
好了,廢話少說,開始正題吧!

Scanner和SplitFunc的工作機(jī)制

package "buffio"
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

Scanner是有緩存的,意思是Scanner底層維護(hù)了一個(gè)Slice用來保存已經(jīng)從Reader中讀取的數(shù)據(jù),Scanner會調(diào)用我們設(shè)置SplitFunc,將緩沖區(qū)內(nèi)容(data)和是否已經(jīng)輸入完了(atEOF)以參數(shù)的形式傳遞給SplitFunc,而SplitFunc的職責(zé)就是根據(jù)上述的兩個(gè)參數(shù)返回下一次Scan需要前進(jìn)幾個(gè)字節(jié)(advance),分割出來的數(shù)據(jù)(token),以及錯(cuò)誤(err)。

這是一個(gè)通信雙向的過程,Scanner告訴我們的SplitFunc已經(jīng)掃描到的數(shù)據(jù)和是否到結(jié)尾了,我們的SplitFunc則根據(jù)這些信息將分割的結(jié)果返回和下次掃描需要前進(jìn)的位置返回給Scanner。用一個(gè)例子來說明:

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í)候會擴(kuò)展為原來的2倍,最大為bufio.MaxScanTokenSize,這樣一開始掃描2個(gè)字節(jié),我們的緩沖區(qū)就滿了,reader的內(nè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)容還沒有讀取到EOF,輸出

false 4 abcd

重復(fù)上述步驟,一直到最后全部內(nèi)容讀取完了,EOF此時(shí)變成了true

true 12 abcdefghijkl

看了上面的過程是不是對SplitFunc的工作原來有了一點(diǎn)理解了呢?再回頭看一下golang的官方文檔有沒有覺得稍微理解了一點(diǎn)?下面是bufio.ScanLines的實(shí)現(xiàn),讀者可以自己研究一下該函數(shù)是如何工作的

標(biāo)準(zhǔn)庫里的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 {
  // 把下次開始讀取的位置向前移動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請求更多的數(shù)據(jù)
 return 0, nil, nil
}

參考

In-depth introduction to bufio.Scanner in Golang

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • 淺談golang類型斷言,失敗類型斷言返回值問題
  • 通過匯編看golang函數(shù)的多返回值問題
  • Golang的func參數(shù)及返回值操作

標(biāo)簽:德宏 吐魯番 東營 常州 貴州 曲靖 保定 許昌

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《golang中bufio.SplitFunc的深入理解》,本文關(guān)鍵詞  golang,中,bufio.SplitFunc,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《golang中bufio.SplitFunc的深入理解》相關(guān)的同類信息!
  • 本頁收集關(guān)于golang中bufio.SplitFunc的深入理解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 榆林市| 长顺县| 岑巩县| 九江市| 宿州市| 九台市| 合山市| 青冈县| 江达县| 平山县| 鲁山县| 司法| 曲松县| 禹州市| 北流市| 当雄县| 沂源县| 凤城市| 锦州市| 富阳市| 西丰县| 开平市| 临西县| 洛川县| 佛坪县| 故城县| 宁安市| 资中县| 鱼台县| 河源市| 名山县| 丘北县| 仁寿县| 上栗县| 环江| 萨迦县| 商水县| 屯昌县| 扎赉特旗| 福海县| 阜康市|