记录一个channel的用法,感觉挺有意思的。
代码语言:go复制package main
import (
"fmt"
"time"
)
type SomeStr struct {
val int
process chan struct{}
}
func SendTask(taskCh chan SomeStr) {
done := make(chan struct{})
taskCh <- SomeStr{val: 100, process: done}
fmt.Println("waitting done")
<-done
fmt.Println("task is over")
}
func main() {
taskCh := make(chan SomeStr)
go SendTask(taskCh)
for {
select {
case v := <-taskCh:
fmt.Println("recv val:", v.val)
close(v.process)
time.Sleep(time.Second)
return
}
}
}