如何在Go中將[]byte轉(zhuǎn)換為io.Reader
在 stackoverflow 上看到一個(gè)問題,題主進(jìn)行了一個(gè)網(wǎng)絡(luò)請(qǐng)求,接口返回的是 []byte。如果想要將其轉(zhuǎn)換成 io.Reader,需要怎么做呢?
這個(gè)問題解決起來并不復(fù)雜,簡(jiǎn)單幾行代碼就可以輕松將其轉(zhuǎn)換成功。不僅如此,還可以再通過幾行代碼反向轉(zhuǎn)換回來。
下面聽我慢慢給你吹,首先直接看兩段代碼。
[]byte 轉(zhuǎn) io.Reader
package main import ( "bytes" "fmt" "log" ) func main() { data := []byte("Hello AlwaysBeta") // byte slice to bytes.Reader, which implements the io.Reader interface reader := bytes.NewReader(data) // read the data from reader buf := make([]byte, len(data)) if _, err := reader.Read(buf); err != nil { log.Fatal(err) } fmt.Println(string(buf)) }
輸出:
Hello AlwaysBeta
這段代碼先將 []byte 數(shù)據(jù)轉(zhuǎn)換到 reader 中,然后再?gòu)?reader 中讀取數(shù)據(jù),并打印輸出。
io.Reader 轉(zhuǎn) []byte
package main import ( "bytes" "fmt" "strings" ) func main() { ioReaderData := strings.NewReader("Hello AlwaysBeta") // creates a bytes.Buffer and read from io.Reader buf := &bytes.Buffer{} buf.ReadFrom(ioReaderData) // retrieve a byte slice from bytes.Buffer data := buf.Bytes() // only read the left bytes from 6 fmt.Println(string(data[6:])) }
輸出:
AlwaysBeta
這段代碼先創(chuàng)建了一個(gè) reader,然后讀取數(shù)據(jù)到 buf,最后打印輸出。
以上兩段代碼就是 []byte 和 io.Reader 互相轉(zhuǎn)換的過程。對(duì)比這兩段代碼不難發(fā)現(xiàn),都有 NewReader 的身影。而且在轉(zhuǎn)換過程中,都起到了關(guān)鍵作用。
那么問題來了,這個(gè) NewReader 到底是什么呢?接下來我們通過源碼來一探究竟。
源碼解析
Go 的 io 包提供了最基本的 IO 接口,其中 io.Reader 和 io.Writer 兩個(gè)接口最為關(guān)鍵,很多原生結(jié)構(gòu)都是圍繞這兩個(gè)接口展開的。
下面就來分別說說這兩個(gè)接口:
Reader 接口
io.Reader 表示一個(gè)讀取器,它將數(shù)據(jù)從某個(gè)資源讀取到傳輸緩沖區(qū)。在緩沖區(qū)中,數(shù)據(jù)可以被流式傳輸和使用。
接口定義如下:
type Reader interface { Read(p []byte) (n int, err error) }
Read() 方法將 len(p) 個(gè)字節(jié)讀取到 p 中。它返回讀取的字節(jié)數(shù) n,以及發(fā)生錯(cuò)誤時(shí)的錯(cuò)誤信息。
舉一個(gè)例子:
package main import ( "fmt" "io" "os" "strings" ) func main() { reader := strings.NewReader("Clear is better than clever") p := make([]byte, 4) for { n, err := reader.Read(p) if err != nil { if err == io.EOF { fmt.Println("EOF:", n) break } fmt.Println(err) os.Exit(1) } fmt.Println(n, string(p[:n])) } }
輸出:
4 Clea
4 r is
4? bet
4 ter
4 than
4? cle
3 ver
EOF: 0
這段代碼從 reader 不斷讀取數(shù)據(jù),每次讀 4 個(gè)字節(jié),然后打印輸出,直到結(jié)尾。
最后一次返回的 n 值有可能小于緩沖區(qū)大小。
Writer 接口
io.Writer 表示一個(gè)編寫器,它從緩沖區(qū)讀取數(shù)據(jù),并將數(shù)據(jù)寫入目標(biāo)資源。
type Writer interface { Write(p []byte) (n int, err error) }
Write 方法將 len(p) 個(gè)字節(jié)從 p 中寫入到對(duì)象數(shù)據(jù)流中。它返回從 p 中被寫入的字節(jié)數(shù) n,以及發(fā)生錯(cuò)誤時(shí)返回的錯(cuò)誤信息。
舉一個(gè)例子:
package main import ( "bytes" "fmt" "os" ) func main() { // 創(chuàng)建 Buffer 暫存空間,并將一個(gè)字符串寫入 Buffer // 使用 io.Writer 的 Write 方法寫入 var buf bytes.Buffer buf.Write([]byte("hello world , ")) // 用 Fprintf 將一個(gè)字符串拼接到 Buffer 里 fmt.Fprintf(&buf, " welcome to golang !") // 將 Buffer 的內(nèi)容輸出到標(biāo)準(zhǔn)輸出設(shè)備 buf.WriteTo(os.Stdout) }
輸出:
hello world ,? welcome to golang !
bytes.Buffer 是一個(gè)結(jié)構(gòu)體類型,用來暫存寫入的數(shù)據(jù),其實(shí)現(xiàn)了 io.Writer 接口的 Write 方法。
WriteTo 方法定義:
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
WriteTo 方法第一個(gè)參數(shù)是 io.Writer 接口類型。
轉(zhuǎn)換原理
再說回文章開頭的轉(zhuǎn)換問題。
只要某個(gè)實(shí)例實(shí)現(xiàn)了接口 io.Reader 里的方法 Read() ,就滿足了接口 io.Reader。
bytes 和 strings 包都實(shí)現(xiàn)了 Read() 方法。
// src/bytes/reader.go // NewReader returns a new Reader reading from b. func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }
// src/strings/reader.go // NewReader returns a new Reader reading from s. // It is similar to bytes.NewBufferString but more efficient and read-only. func NewReader(s string) *Reader { return &Reader{s, 0, -1} }
在調(diào)用 NewReader 的時(shí)候,會(huì)返回了對(duì)應(yīng)的 T.Reader 類型,而它們都是通過 io.Reader 擴(kuò)展而來的,所以也就實(shí)現(xiàn)了轉(zhuǎn)換。
總結(jié)
在開發(fā)過程中,避免不了要進(jìn)行一些 IO 操作,包括打印輸出,文件讀寫,網(wǎng)絡(luò)連接等。
在 Go 語言中,也提供了一系列標(biāo)準(zhǔn)庫(kù)來應(yīng)對(duì)這些操作,主要封裝在以下幾個(gè)包中:
- io:基本的 IO 操作接口。
- io/ioutil:封裝了一些實(shí)用的 IO 函數(shù)。
- fmt:實(shí)現(xiàn)了 IO 格式化操作。
- bufio:實(shí)現(xiàn)了帶緩沖的 IO。
- net.Conn:網(wǎng)絡(luò)讀寫。
- os.Stdin,os.Stdout:系統(tǒng)標(biāo)準(zhǔn)輸入輸出。
- os.File:系統(tǒng)文件操作。
- bytes:字節(jié)相關(guān) IO 操作。
除了 io.Reader 和 io.Writer 之外,io 包還封裝了很多其他基本接口,比如 ReaderAt,WriterAt,ReaderFrom 和 WriterTo 等,這里就不一一介紹了。這部分代碼并不復(fù)雜,讀起來很輕松,而且還能加深對(duì)接口的理解,推薦大家看看。
到此這篇關(guān)于如何在Go中將[]byte轉(zhuǎn)換為io.Reader的文章就介紹到這了,更多相關(guān)Go []byte轉(zhuǎn)換為io.Reader內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。