2
我正在尝试net.Pipe()
。我认为编写"haha"
字符串,然后阅读它可能是一个很好的实验。我该如何正确编写'Read`和`Write` net.Pipe()`
这是我的第一个版本。在Write
func TestNetPipe(t *testing.T) {
out1 := make([]byte, 10)
c1, c2 := net.Pipe()
c1.Write([]byte("haha"))
c2.Read(out1)
}
它阻止我试图用一个够程
func TestNetPipe(t *testing.T) {
out1 := make([]byte, 10)
c1, c2 := net.Pipe()
go func() {
c1.Write([]byte("haha"))
}()
fmt.Printf("%v\n", out1)
c2.Read(out1)
fmt.Printf("%v\n", out1)
}
它的工作原理。但我觉得不能保证Read
会读取整个"haha"
字符串。它可能只能读取"hah"
部分。
我不知道是否有更好的方法来从包io/ioutil
演示net.Pipe()