SoFunction
Updated on 2025-03-05

Solve the problem of golang not executing

goroutine doesn't seem to have to be explained too much. It can be used to implement multi-threading, or it can be used to implement asynchronous events.

In the process of using the keyword go, the following code will often be used.

package main
import (
	"fmt"
	"sync"
	"time"
)
func Run() {
	var wg = &{}
	go func() {
		(1)
		("halo world start")
		( * 5)
		("halo world end")
		()
	}()
	// ( * 5)
	// ("server will start")
	()
}
func main() {
	Run()
}
// output:
// 

The expected result is to print halo world start, and print halo world end in 5 seconds, but the result is nothing, and the process ends immediately.

reason

The keyword go is asynchronous. When it is executed to go, the contents behind go will not be executed immediately, and continue to be executed. At this time (1) before there is time to execute, () is already executed, that is, no waiting will occur and the process ends.

How to solve it:

Just have to have other operations before () and give enough time to (1) to execute.

Method 1. Wait for time, add a sentence (*5) before () to not affect performance, but also allows (1) to be executed in time.

Method 2: There are IO operations. There are other IO operations in (). For example ("server will start"). The reason is that the output of std will turn the process from user state to kernel state. After the printing command is issued, it will switch back to user state. The conversion of this state is very consumed, and (1) there is time to execute.

Don't worry

Is there any concern? Method 1 waits for time, and the waiting time is not long enough, so (1) still has no time to execute. don't worry.

This involves the scheduling issue of goroutine. During the execution of the go process, one must be taken out of the goroutine queue for execution. When () is executed, even if (), one nanosecond, one...one nanosecond, (1) has time to execute, because the main goroutine will be switched to sleep state. The go process must take a thread to execute, and it will get (1) this thread. Then it will be natural.

At the same time, method 2 is also similar. When a printed event is issued, the entire process will be switched to the ready state and then executed by CPU.

Supplement: The wait() call location of [golang]{} results in unexpected errors

There are so many coroutines, and I always feel that I have all the world. If I have nothing to do, I like to go to a coroutine. I can do it at will and write a similar code in the project:

  wh := {}
  out := make(chan string)
  go func() {
    ()
    close(out)
  }()
  go func() {
    for i := 0; i < 2; i++ {
      (1)
      go tt(out)
      ()
    }
  }()

I thought about opening a coroutine to wait for all coroutine groups and test it through. No problem, it’s so awesome, coroutines! !

You can test multiple times and even if:

send close channel

Or the coroutine is determined to die in one, and he also thought that the sub-method he wrote was probably accidentally closed the channel. After searching for a long time, he only found it after (). After deducting it for a long time, I didn't expect the mistake of adding anything to my own tricks. After the boss gave me some advice, I found out that I had opened a coroutine. Before the latter coroutine could perform (1) operation, () had passed and closed the channel.

I had to write honestly:

  wh := {}
  out := make(chan string)
  go func() {
    for i := 0; i < 2; i++ {
      (1)
      go tt(out)
      ()
    }
    ()
    close(out)
  }()

In fact, it is just a small synchronization problem, and the bystanders are clear! ! !

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.