SoFunction
Updated on 2025-03-01

golang prevents the main goroutine from exiting

1:for

 //Use wireless loop for{
 }

If you want to exit

 for {
    reutrn 
 }
 

For example: start three goroutines and wait for three goroutines to end the execution. Exit the main goroutine

var c bool = false
var nums int = 0
for i := 0; i < 3; i++ {
go func() {
("begin------------end")
(10 * )
nums++
if nums == 2 {
c = true
}
}()
}
for {
if c == true {
return
}
}

2:chan

var c = make(chan bool)
........
.......
<- c

If you want to exit the main process,

c <- true

For example: start three goroutines and wait for three goroutines to end the execution. Exit the main goroutine

var c = make(chan bool)
var nums int = 0
for i := 0; i < 3; i++ {
go func() {
("begin------------")
(10 * )
nums++
if nums == 2 {
c <- true
}
}()
}
<-c

Supplement: Use channel to limit the number of goroutines

Although golang is cheap to start a goroutine, it is not unlimitedly used.

Most of them use channels to limit the number of goroutines

I wrote a test DEMO as follows:

package main 
import (
 "fmt"
 "runtime"
 "time"
)
 
var (
 chanNum   = 3 //The number of startups readChan  = make(chan int)        //The channel of operation information limitChan = make(chan bool, 1000) //Limit the number of goroutine channels, limit 1,000 here)
 
//Initial methodfunc init() {
 ("init")
 
 for i := 0; i &lt; chanNum; i++ {
  go Queue(i, readChan) //Open the working pool }
}
 
func main() {
 ("main")
 
 //Enable a go method and fill data into readChan without limit go func() {
  for {
   readChan &lt;- 1
  }
 }()
 
 //The program exits after listening to the keyboard event var input string
 (&amp;input)
}
 
//Work Poolfunc Queue(qid int, rchan chan int) {
 var dat int
 t := () //Timer, one second for {
  select {
  case d := &lt;-rchan:
   limitChan &lt;- true //The buffer is full and the subsequent readChan will wait   dat += d
   go showNum(qid, dat) //Every time a data is received from the channel, a goroutine is started. LimitChan will limit the number of goroutines.  case &lt;-t:
   showGoNum(qid) //Timer, print the current number of goroutines once a second  }
 }
}
 
func showNum(qid, i int) {
 //After the processing is successful, get a data in the Channel buffer that restricts goroutine, and limitChan can be written again //Use Defer to make sure that a buffer of limitChan is released defer func() {
  &lt;-limitChan
 }()
 ( * 100) //Simulation program processing time consuming //(qid, "===========", i)
}
 
//Show the current goroutine numberfunc showGoNum(qid int) {
 ("%d====numGo:==%d\n", qid, ())
}

The execution results are as follows:

go run channel_limit_goroutine.go

init

main

0====numGo:==1004

1====numGo:==1003

2====numGo:==1005

1====numGo:==1005

0====numGo:==1005

2====numGo:==1005

0====numGo:==1005

1====numGo:==1005

2====numGo:==1005

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.