Skip to content

Commit

Permalink
feat(concurrency): add channel
Browse files Browse the repository at this point in the history
  • Loading branch information
shgopher committed Jan 22, 2024
1 parent 093beae commit 7113ddd
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions 并发/channel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,27 @@ fan out 跟 fan in 模式是相反的,fan out 指的是拥有一个输入源
下面看一下代码:

```go
func fanout(value chan any,out []chan any,async bool){

func fanout(value chan any, out []chan any, async bool) {
go func() {
defer func() {
for i := 0; i < len(out); i++ {
close(out[i])
}
}()
// 对一个nil的通道进行 for range 遍历会导致阻塞(block)。
for v := range value {
for vi := 0; vi < len(out); vi++ {
vi := vi // if go version is lower then 1.22
if async {
go func() {
out[vi] <- v
}()
} else {
out[vi] <- v
}
}
}
}()
}
```

Expand Down

0 comments on commit 7113ddd

Please # to comment.