簡體   English   中英

有沒有辦法將[]字節切換轉換為io.Reader?

[英]Is there a way in go to convert a []byte slice to an io.Reader?


我剛開始使用go並且想知道,是否可以將[]字節切片轉換為io.Reader。 如ioutil.ReadAll所示,可以使用其他方式。
如果不是可以使用code.google.com/p/go.net/html.Tokenizer以某種方式使用字節切片?

是的: bytes.NewBuffer

io.Reader示例:

http://play.golang.org/p/P0VbE8UFpC

package main

import (
    "bytes"
    "encoding/base64"
    "io"
    "os"
)

func main() {
    // A Buffer can turn a string or a []byte into an io.Reader.
    buf := bytes.NewBuffer([]byte("R29waGVycyBydWxlIQ=="))
    dec := base64.NewDecoder(base64.StdEncoding, buf)
    io.Copy(os.Stdout, dec)
}

您可以在bytes包中使用NewReader:

in := bytes.NewReader(b []byte)

https://golang.org/pkg/bytes/#NewReader

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM