package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strconv"
"time"
)
func readData(bfRd io.Reader, dataChan chan []byte, endChan chan error) {
buf := make([]byte, 100)
for {
n, err := bfRd.Read(buf)
if err != nil {
endChan <- err
return
}
fmt.Printf("read data %s size \n", strconv.Itoa(n))
dataChan <- buf
}
}
func main() {
file, err := os.Open("client.go")
if err != nil {
log.Fatalln(err)
}
var data []byte
bfRd := bufio.NewReader(file)
var dataChan = make(chan []byte)
var endChan = make(chan error)
go readData(bfRd, dataChan, endChan)
for {
select {
case <-endChan:
fmt.Println(<-endChan)
break
case <-dataChan:
data = append(data, <-dataChan...)
case <-time.After(time.Second):
fmt.Println("timeout")
break
default:
fmt.Println("channel cannot use")
}
}
fmt.Println(string(data))
}
因为你的代码里,都是调用两次 *<-dataChan* 和 *<-errChan* ,dataChan还好,数据持续进入,而errChan只接受到一次数据,而你想取两次数据,然后就思索了。
#9
更多评论
基于你的代码修改,首先`fmt.Println(<-endChan)`会造成`deadlock`,然后`select`中的`break`并不能跳出外层`for`循环,代码如下:
```go
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strconv"
"time"
)
func readData(bfRd io.Reader, dataChan chan []byte, endChan chan error) {
buf := make([]byte, 100)
for {
n, err := bfRd.Read(buf)
if err != nil {
endChan <- err
return
}
fmt.Printf("read data %s size \n", strconv.Itoa(n))
dataChan <- buf
}
}
func main() {
file, err := os.Open("client.go")
if err != nil {
log.Fatalln(err)
}
var data []byte
bfRd := bufio.NewReader(file)
var dataChan = make(chan []byte)
var endChan = make(chan error)
go readData(bfRd, dataChan, endChan)
L:
for {
select {
case err := <-endChan:
fmt.Println(err)
break L
case <-dataChan:
data = append(data, <-dataChan...)
case <-time.After(time.Second):
fmt.Println("timeout")
break L
default:
fmt.Println("channel cannot use")
}
}
fmt.Println(string(data))
}
```
#2