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))
}
<a href="/user/brantou" title="@brantou">@brantou</a> <a href="/user/Hubery" title="@Hubery">@Hubery</a> 嗯,`buf := make([]byte, 100)`应该放在`for`循环中的,一开始只是想回答题主的死锁问题,代码的确有问题
#7
更多评论
基于你的代码修改,首先`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