首页 > 其他分享 >Go 语言并发

Go 语言并发

时间:2023-08-14 21:25:38浏览次数:37  
标签:wg 语言 fmt sync 并发 func Go main hello

启动单个goroutine

package main

import (
"fmt"
"time"
)

func hello(){
fmt.Println("hello")
}

func main() {
go hello()
fmt.Println("欢迎来到编程狮")
time.Sleep(time.Second)
}

sync.WaitGroup

package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func hello() {
fmt.Println("hello")
defer wg.Done()//把计算器-1
}

func main() {
wg.Add(1)//把计数器+1
go hello()
fmt.Println("欢迎来到编程狮")
wg.Wait()//阻塞代码的运行,直到计算器为0
}

 

启动多个goroutine

package main

import (
"fmt"
"sync"
)

var wg sync.WaitGroup

func hello(i int) {
fmt.Printf("hello,欢迎来到编程狮%v\n", i)
defer wg.Done()//goroutine结束计数器-1
}

func main() {
for i := 0; i < 10; i++ {
go hello(i)
wg.Add(1)//启动一个goroutine计数器+1
}
wg.Wait()//等待所有的goroutine执行结束
}

标签:wg,语言,fmt,sync,并发,func,Go,main,hello
From: https://www.cnblogs.com/fczlm/p/17629766.html

相关文章

  • Go 错误处理
     Go语言通过内置的错误接口提供了非常简单的错误处理机制。error类型是一个接口类型typeerrorinterface{Error()string}packagemainimport("fmt")//定义一个DivideError结构typeDivideErrorstruct{divideeintdividerint}//实现`error`......
  • Go 语言反射(Reflect)
    Go语言提供了一种机制,在不知道具体类型的情况下,可以用反射来更新变量值,查看变量类型Typeofpackagemainimport( "fmt" "reflect")funcmain(){ varbooknumfloat32=6 varisbookbool=true bookauthor:="www.w3cschool.cn" bookdetail:=make(map[string]string) bo......
  • 13 桥接模式 -- go语言设计模式
    桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(HandleandBody)模式或接口(interface)模式。桥接模式的实现代码packagemainimport"fmt"//发送信息的具体实现(操作)typeMessageImplementerinterface{ send(test,......
  • 让 GPT-4 来修复 Golang “数据竞争”问题 - 每天5分钟玩转 GPT 编程系列(6)
    目录1.Golang中的“数据竞争”2.GoPool中的数据竞争问题3.让GPT-4来修复数据竞争问题3.1和GPT-4的第一轮沟通3.2和GPT-4的第二轮沟通3.3提交代码4.总结1.Golang中的“数据竞争”我在上个月发过一篇《跟着GPT-4从0到1学习Golang并发机制(三)》,文中有一节专......
  • E. Maximum Monogonosity
    E.MaximumMonogonosityYouaregivenanarray$a$oflength$n$andanarray$b$oflength$n$.Thecostofasegment$[l,r]$,$1\lel\ler\len$,isdefinedas$|b_l-a_r|+|b_r-a_l|$.Recallthattwosegments$[l_1,r_1]$,$1\lel_1\ler......
  • Go 语言范围(Range)
    range关键字用于for循环中迭代数组(array)、切片(slice)、通道(channel)或集合(map)的元素。packagemainimport"fmt"funcmain(){//这是我们使用range去求一个slice的和。使用数组跟这个很类似nums:=[]int{2,3,4}sum:=0for_,num:=rangenums{......
  • Go 语言Map(集合)
    定义Map/*声明变量,默认map是nil*/varmap_variablemap[key_data_type]value_data_type/*使用make函数*/map_variable=make(map[key_data_type]value_data_type) packagemainimport"fmt"funcmain(){varcountryCapitalMapmap[string]string/*创建......
  • Go 语言递归函数
    递归,就是在运行的过程中调用自己。阶乘packagemainimport"fmt"funcFactorial(xint)(resultint){ifx==0{result=1}else{result=x*Factorial(x-1)}return}funcmain(){variint=15fmt.Printf("%d的阶乘是%d\n",i......
  • Go 语言类型转换
    packagemainimport"fmt"funcmain(){varsumint=17varcountint=5varmeanfloat32mean=float32(sum)/float32(count)fmt.Printf("mean的值为:%f\n",mean)}go不支持隐式转换类型错:packagemainimport"fmt"funcmain(......
  • Go 语言接口
    Go语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口。 packagemainimport("fmt")typePhoneinterface{call()}typeNokiaPhonestruct{}func(nokiaPhoneNokiaPhone)call(){fmt......