go-channel空間不夠怎麼辦
阿新 • • 發佈:2019-02-16
一句話總結:channel空間不夠儲存到其他地方,等有空間再儲存儘量
package main
import "fmt"
func main() {
memoryMsgChan := make(chan int, 4)
s := []int{1,2,3,4,5,6,7}
for _, msg := range s {
select {
case memoryMsgChan <- msg:
default:
fmt.Println("chan full:", msg)
}
}
var a int = 11
for i:=0; i<5;i++ {
select {
case a = <-memoryMsgChan:
fmt.Println("a:", a)
default:
fmt.Println("chan empty")
}
}
}
chan full: 5
chan full: 6
chan full: 7
a: 1
a: 2
a: 3
a: 4
chan empty
Process finished with exit code 0