原文鏈接: 如何在 Go 中將 []byte 轉換為 io.Reader?
在 stackoverflow 上看到一個問題,題主進行了一個網絡請求,接口返回的是 []byte
。如果想要將其轉換成 io.Reader
,需要怎么做呢?
這個問題解決起來並不復雜,簡單幾行代碼就可以輕松將其轉換成功。不僅如此,還可以再通過幾行代碼反向轉換回來。
下面聽我慢慢給你吹,首先直接看兩段代碼。
[]byte 轉 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
數據轉換到 reader
中,然后再從 reader
中讀取數據,並打印輸出。
io.Reader 轉 []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
這段代碼先創建了一個 reader
,然后讀取數據到 buf
,最后打印輸出。
以上兩段代碼就是 []byte
和 io.Reader
互相轉換的過程。對比這兩段代碼不難發現,都有 NewReader
的身影。而且在轉換過程中,都起到了關鍵作用。
那么問題來了,這個 NewReader
到底是什么呢?接下來我們通過源碼來一探究竟。
源碼解析
Go 的 io
包提供了最基本的 IO 接口,其中 io.Reader
和 io.Writer
兩個接口最為關鍵,很多原生結構都是圍繞這兩個接口展開的。
下面就來分別說說這兩個接口:
Reader 接口
io.Reader
表示一個讀取器,它將數據從某個資源讀取到傳輸緩沖區。在緩沖區中,數據可以被流式傳輸和使用。
接口定義如下:
type Reader interface {
Read(p []byte) (n int, err error)
}
Read()
方法將 len(p)
個字節讀取到 p
中。它返回讀取的字節數 n
,以及發生錯誤時的錯誤信息。
舉一個例子:
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
不斷讀取數據,每次讀 4 個字節,然后打印輸出,直到結尾。
最后一次返回的 n 值有可能小於緩沖區大小。
Writer 接口
io.Writer
表示一個編寫器,它從緩沖區讀取數據,並將數據寫入目標資源。
type Writer interface {
Write(p []byte) (n int, err error)
}
Write
方法將 len(p)
個字節從 p
中寫入到對象數據流中。它返回從 p
中被寫入的字節數 n
,以及發生錯誤時返回的錯誤信息。
舉一個例子:
package main
import (
"bytes"
"fmt"
"os"
)
func main() {
// 創建 Buffer 暫存空間,並將一個字符串寫入 Buffer
// 使用 io.Writer 的 Write 方法寫入
var buf bytes.Buffer
buf.Write([]byte("hello world , "))
// 用 Fprintf 將一個字符串拼接到 Buffer 里
fmt.Fprintf(&buf, " welcome to golang !")
// 將 Buffer 的內容輸出到標准輸出設備
buf.WriteTo(os.Stdout)
}
輸出:
hello world , welcome to golang !
bytes.Buffer
是一個結構體類型,用來暫存寫入的數據,其實現了 io.Writer
接口的 Write
方法。
WriteTo
方法定義:
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)
WriteTo
方法第一個參數是 io.Writer
接口類型。
轉換原理
再說回文章開頭的轉換問題。
只要某個實例實現了接口 io.Reader
里的方法 Read()
,就滿足了接口 io.Reader
。
bytes
和 strings
包都實現了 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} }
在調用 NewReader
的時候,會返回了對應的 T.Reader
類型,而它們都是通過 io.Reader
擴展而來的,所以也就實現了轉換。
總結
在開發過程中,避免不了要進行一些 IO 操作,包括打印輸出,文件讀寫,網絡連接等。
在 Go 語言中,也提供了一系列標准庫來應對這些操作,主要封裝在以下幾個包中:
io
:基本的 IO 操作接口。io/ioutil
:封裝了一些實用的 IO 函數。fmt
:實現了 IO 格式化操作。bufio
:實現了帶緩沖的 IO。net.Conn
:網絡讀寫。os.Stdin
,os.Stdout
:系統標准輸入輸出。os.File
:系統文件操作。bytes
:字節相關 IO 操作。
除了 io.Reader
和 io.Writer
之外,io
包還封裝了很多其他基本接口,比如 ReaderAt
,WriterAt
,ReaderFrom
和 WriterTo
等,這里就不一一介紹了。這部分代碼並不復雜,讀起來很輕松,而且還能加深對接口的理解,推薦大家看看。
好了,本文就到這里吧。關注我,帶你通過問題讀 Go 源碼。
推薦閱讀:
熱情推薦:
- 計算機經典書籍(含下載方式)
- 技術博客: 硬核后端技術干貨,內容包括 Python、Django、Docker、Go、Redis、ElasticSearch、Kafka、Linux 等。
- Go 程序員: Go 學習路線圖,包括基礎專欄,進階專欄,源碼閱讀,實戰開發,面試刷題,必讀書單等一系列資源。
- 面試題匯總: 包括 Python、Go、Redis、MySQL、Kafka、數據結構、算法、編程、網絡等各種常考題。
參考文章: