关于go:不背锅运维上篇Go并发编程

0次阅读

共计 2413 个字符,预计需要花费 7 分钟才能阅读完成。

  1. 根本应用
package main

import (
 "fmt"
 "sync"
)

var wg sync.WaitGroup

func hello() {fmt.Println("hello func...")
 wg.Done() // 告诉计数器减 1}

func main() {wg.Add(4) // 计数器,4 个并发工作
 go hello()
 go hello()
 go hello()
 go hello()
 fmt.Println("main func!")

 wg.Wait() // 期待所有工作执行实现}
  1. 革新一下,开启 10000 个 goroutine
package main

import (
 "fmt"
 "sync"
)

var wg sync.WaitGroup

func hello(i int) {fmt.Println("hello func...", i)
 wg.Done()}

func main() {// wg.Add(10000)
 for i := 0; i < 10000; i++ {wg.Add(1)
  go hello(i)
 }
 fmt.Println("main func!")

 wg.Wait()}
  1. 将上一个例子革新成匿名函数
package main

import (
 "fmt"
 "sync"
)

var wg sync.WaitGroup

func main() {// wg.Add(10000)
 for i := 0; i < 10000; i++ {go func(i int) {fmt.Println("hello...", i)
  }(i)
 }
 fmt.Println("main func!")

 wg.Wait()}
  1. 指定占用 CPU 外围数
package main

import (
 "fmt"
 "runtime"
 "sync"
)

var wg sync.WaitGroup

func test1() {
 for i := 0; i < 10; i++ {fmt.Println("func test1...", i)
 }
}

func test2() {
 for i := 0; i < 10; i++ {fmt.Println("func test2...", i)
 }
}

func main() {runtime.GOMAXPROCS(1) // 只占用 1 个 CPU 外围
 wg.Add(2)
 go test1()
 go test2()
 wg.Wait()}
  1. 带缓冲区的通道,相似于异步的操作
package main

import "fmt"

func main() {ch1 := make(chan int, 1) // 只能寄存 1 个值的缓冲区通道
 ch1 <- 10  // 发送
 x := <-ch1 // 接管
 fmt.Println(x)
 close(ch1)
}
  1. 无缓冲区的通道,又称为同步通道
package main

import "fmt"

func main() {ch1 := make(chan int) // 无缓冲区通道,又称为同步通道,ch1 <- 10             // 此时这里会处于阻塞的状态,除非有另外一个 goroutine 去取值,它才会发送
 x := <-ch1
 fmt.Println(x)
 close(ch1)
}
  1. 获取通道的容量和通道里的元素数量
package main

import "fmt"

func main() {ch1 := make(chan int, 10)
 ch1 <- 89
 ch1 <- 70
 fmt.Println(len(ch1)) // 获取通道中元素的数量
 fmt.Println(cap(ch1)) // 获取通道的容量
 close(ch1)
}
  1. 通道和 goroutine 的小栗子
package main

import (
 "fmt"
 "sync"
)

type myinterface interface{}

var ch1 = make(chan myinterface, 1)
var wg sync.WaitGroup

func sendData(i myinterface) {fmt.Printf("向通道发送 %v 胜利 \n", i)
 ch1 <- i
 wg.Done()}

func readData() {
 v := <-ch1
 fmt.Println("从通道获取的值:", v)
 wg.Done()}

func main() {nameArray := []string{"ttr", "tantianran"}

 wg.Add(2)
 go sendData(nameArray)
 go readData()
 wg.Wait()}
  1. 通道 +goroutine,实现协同干活例子 2
package main

import "fmt"

func producer(ch chan int) {
 for i := 0; i < 10; i++ {ch <- i}
 close(ch)
}

func consumer(ch1 chan int, ch2 chan int) {
 for {
  v, ok := <-ch1
  if !ok {break}
  ch2 <- v * 2
 }
 close(ch2)
}

func main() {ch1 := make(chan int, 100)
 ch2 := make(chan int, 200)

 go producer(ch1)
 go consumer(ch1, ch2)

 for i := range ch2 {fmt.Println(i)
 }
}

本文转载于(喜爱的盆友关注咱们哦):https://mp.weixin.qq.com/s/_X…

正文完
 0