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))
}
基于你的代码修改,首先`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
更多评论
```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[:n]
}
}
func main() {
file, err := os.Open("/Users/abin/data/e/dev1/server/src/lab/lab1.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)
var d []byte
L:
for {
select {
case err := <-endChan:
fmt.Println(err)
break L
case d = <-dataChan:
data = append(data, d...)
case <-time.After(time.Second):
fmt.Println("timeout")
break L
default:
fmt.Println("channel cannot use")
}
}
fmt.Println(string(data))
}
```
#3