goroutine与goroutine之间的双向通道就是channel
package main
import "fmt"
func chanDemmo() {
c := make(chan int)
c <- 1
c <- 2
n := <-c
fmt.Println(n)
}
func main() {
chanDemmo()
}
运行程序,会出现死锁;channel是goroutine与goroutine之间的交互,如果没有goroutine就会出现死锁
package main
import (
"fmt"
"time"
)
func chanDemmo() {
c := make(chan int)
go func() {
for {
n := <-c
fmt.Println(n)
}
}()
c <- 1
c <- 2
time.Sleep(time.Millisecond)
}
func main() {
chanDemmo()
}
package main
import (
"fmt"
"time"
)
func chanDemmo() {
c := make(chan int)
go worker(c)
c <- 1
c <- 2
time.Sleep(time.Millisecond)
}
func worker(c chan int) {
for {
n := <-c
fmt.Println(n)
}
}
func main() {
chanDemmo()
}
package main
import (
"fmt"
"time"
)
func chanDemmo() {
var channels [10]chan int
for i := 0; i < 10; i++ {
channels[i] = make(chan int)
go worker(i, channels[i])
}
for i := 0; i < 10; i++ {
channels[i] <- i
}
time.Sleep(time.Millisecond)
}
func worker(id int, c chan int) {
for {
fmt.Printf("Worker %d received %d\n", id, <-c)
}
}
func main() {
chanDemmo()
}
package main
import (
"fmt"
"time"
)
func createChan() chan int {
c:=make(chan int)
return c
}
func chanDemmo() {
var channels [10]chan int
for i := 0; i < 10; i++ {
channels[i] = createChan()
go worker(i, channels[i])
}
for i := 0; i < 10; i++ {
channels[i] <- i
}
time.Sleep(time.Millisecond)
}
func worker(id int, c chan int) {
for {
fmt.Printf("Worker %d received %d\n", id, <-c)
}
}
func main() {
chanDemmo()
}
package main
import (
"fmt"
"time"
)
func createChan() chan int {
c := make(chan int)
return c
}
func createSendChan() chan<- int {
c := make(chan int)
return c
}
func chanDemmo() {
var channels [10]chan<- int
for i := 0; i < 10; i++ {
channels[i] = createSendChan()
// go worker(i, channels[i])
}
for i := 0; i < 10; i++ {
channels[i] <- i
}
time.Sleep(time.Millisecond)
}
func worker(id int, c chan int) {
for {
fmt.Printf("Worker %d received %d\n", id, <-c)
}
}
func main() {
chanDemmo()
}
我们再createSendChan函数里面规定了返回的chan只能取发数据,如果我们用返回的chan收数据程序会编译不通过
再最开始的例子里面说,如果没有goroutine来接收,会出现死锁,但是我们可以再创建chan的时候设置一个缓冲区,这样的话,发送内容的时候就会发送到缓冲区里面,只有超出了缓冲区大小时才会出现死锁
package main
func main() {
bufferedChan()
}
func bufferedChan() {
c:=make(chan int,3)
c <- 1
c <- 2
c <- 3
}
package main
func main() {
//chanDemmo()
bufferedChan()
}
func bufferedChan() {
c:=make(chan int,3)
c <- 1
c <- 2
c <- 3
c <- 3
}
package main
import (
"fmt"
"time"
)
func worker(id int, c chan int) {
for {
fmt.Printf("Worker %d received %d\n", id, <-c)
}
}
func main() {
bufferedChan()
}
func bufferedChan() {
c:=make(chan int,3)
go worker(0,c)
c <- 1
c <- 2
c <- 3
c <- 4
time.Sleep(time.Millisecond)
}
package main
import (
"fmt"
"time"
)
func main() {
bufferedChan()
}
func bufferedChan() {
c:=make(chan int,3)
go workerClose(0,c)
c <- 1
c <- 2
c <- 3
c <- 4
close(c)
time.Sleep(time.Millisecond)
}
func workerClose(id int, c chan int) {
for {
i,ok := <-c
if !ok {
break
}
fmt.Printf("Worker %d received %d\n", id, i)
}
}
package main
import (
"fmt"
"time"
)
func main() {
bufferedChan()
}
func bufferedChan() {
c:=make(chan int,3)
go workerClose(0,c)
c <- 1
c <- 2
c <- 3
c <- 4
close(c)
time.Sleep(time.Millisecond)
}
func workerClose(id int, c chan int) {
for i:= range c{
fmt.Printf("Worker %d received %d\n", id, i)
}
}