Google资深工程师深度讲解Go语言-channel 通道 (十)

举报
lxw1844912514 发表于 2022/03/27 00:03:58 2022/03/27
【摘要】 一.channel channelbuffered channelrange.由发送方结束发送理论基础:communication sequential process(csp)不要通过共享内存来通信;通过通信来共享内存 package main import ( "fmt" "time") func chanDemo() { c :...

一.channel

  • channel
  • buffered channel
  • range.由发送方结束发送
  • 理论基础:communication sequential process(csp)
  • 不要通过共享内存来通信;通过通信来共享内存

  
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func chanDemo() {
  7. c := make(chan int)
  8. go func() {
  9. for {
  10. n := <-c
  11. fmt.Println(n)
  12. //Output:
  13. // 1
  14. //2
  15. }
  16. }()
  17. c <- 1
  18. c <- 2
  19. time.Sleep(time.Microsecond)
  20. }
  21. func main() {
  22. chanDemo()
  23. }

  
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func worker(id int, c chan int) {
  7. for n := range c {
  8. //n := <-c //收数据
  9. //n, ok := <-c
  10. //if !ok {
  11. // break
  12. //}
  13. fmt.Printf("worker %d received %d\n", id, n)
  14. //fmt.Println(n)
  15. //Output:
  16. // 1
  17. //2
  18. }
  19. }
  20. func createWorker(id int, ) chan<- int { //返回channel
  21. c := make(chan int)
  22. go worker(id, c)
  23. return c
  24. }
  25. func chanDemo() {
  26. //var c chan int //变量c 是个chanel 里面内容是int
  27. //c := make(chan int)
  28. var channels [10]chan<- int
  29. for i := 0; i < 10; i++ {
  30. channels[i] = createWorker(i)
  31. }
  32. for i := 0; i < 10; i++ {
  33. channels[i] <- 'a' + i
  34. }
  35. for i := 0; i < 10; i++ {
  36. channels[i] <- 'A' + i
  37. }
  38. //c <- 1 //发数据
  39. //c <- 2
  40. time.Sleep(time.Microsecond)
  41. }
  42. func bufferedChannel() {
  43. c := make(chan int, 3)
  44. go worker(0, c)
  45. c <- 'a'
  46. c <- 'b'
  47. c <- 'c'
  48. c <- 'd'
  49. time.Sleep(time.Microsecond)
  50. }
  51. func channelClose() { //发送方通知接收方
  52. c := make(chan int, 3)
  53. go worker(0, c)
  54. c <- 'a'
  55. c <- 'b'
  56. c <- 'c'
  57. c <- 'd'
  58. close(c)
  59. time.Sleep(time.Microsecond)
  60. }
  61. func main() {
  62. fmt.Println("channel ad first-class citizen")
  63. chanDemo()
  64. fmt.Println("Buffered channel")
  65. bufferedChannel()
  66. fmt.Println("Channel close and range")
  67. channelClose()
  68. }

二.传统同步机制

  • waitGroup :等待组
  • Mutex 互斥
  • Cond

  
  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )
  7. type atomicInt struct {
  8. value int
  9. lock sync.Mutex
  10. }
  11. func (a *atomicInt)increment() {
  12. a.lock.Lock()
  13. defer a.lock.Unlock()
  14. a.value++
  15. }
  16. func (a *atomicInt)get()int {
  17. a.lock.Lock()
  18. defer a.lock.Unlock()
  19. return a.value
  20. }
  21. func main() {
  22. var a atomicInt
  23. a.increment()
  24. go func() {
  25. a.increment()
  26. }()
  27. time.Sleep(time.Second)
  28. fmt.Println(a.get())
  29. }

 

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/108448296

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。