我刚开始学习Go语言,想知道是否可以将[]byte切片转换为io.Reader。像ioutil.ReadAll一样,反过来是可以的。
如果不行,是否可以通过code.google.com/p/go.net/html.Tokenizer和字节切片进行某种形式的处理?
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.NewBuffer
有什么不同? - Fred Hors
bytes.NewReader
有什么区别? - Fred Hors// Unlike a Buffer, a Reader is read-only and supports seeking.
。我想问题应该是“你的使用场景是什么?” - Matt Oestreich