2 回答
TA贡献1111条经验 获得超0个赞
它们是不同的类型。但是,abufio.ReadWriter包含一个指向bufio.Reader类型和bufio.Writer类型的指针,作为其结构的元素。因此,传递正确的密码应该很容易。试试这个:
func test(sl bufio.ReadWriter){
readStuff(sl.Reader)
[...]
writeStuff(sl.Writer)
}
// Changed this bufio.Reader to a pointer receiver
func readStuff(sl *bufio.Reader) {
[...]
}
TA贡献1789条经验 获得超10个赞
bufio.ReadWriter是具体类型,而不是接口。但是,它确实满足接口(io.ReadWriter),因此可以将其分配给适当接口类型的变量/函数自变量。然后,它将按您预期的方式工作(您的代码实际上不使用任何接口):
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
)
func readStuff(r io.Reader) {
b := make([]byte, 10)
n, err := r.Read(b)
if err != nil && err != io.EOF {
log.Fatal(err)
}
fmt.Printf("readStuff: %q\n", b[:n])
}
func writeStuff(w io.Writer) {
b := []byte("written")
n, err := w.Write(b)
if n != len(b) {
log.Fatal("Short write")
}
if err != nil {
log.Fatal(err)
}
}
func test(rw io.ReadWriter) {
readStuff(rw)
writeStuff(rw)
}
func main() {
r := io.Reader(bytes.NewBufferString("source"))
var uw bytes.Buffer
w := io.Writer(&uw)
rw := bufio.NewReadWriter(bufio.NewReader(r), bufio.NewWriter(w))
test(rw)
rw.Flush()
fmt.Printf("The underlying bytes.Buffer writer contains %q\n", uw.Bytes())
}
(也在这里)
输出:
readStuff: "source"
The underlying bytes.Buffer writer contains "written"
这种方式test可以消耗任何东西io.ReadWriter,而不仅仅是特定的东西。这暗示了您有关“哲学”问题的提示。
- 2 回答
- 0 关注
- 181 浏览
添加回答
举报