I’d like to share a collection of ways to block Go programs from exiting, and there are still some brainy ways to do it.
In a program like the one below, the program exits as soon as it runs. The reason is that the main goroutine is finished executing, and the child goroutine exists, but it fails to block the main goroutine’s execution.
I’ve compiled a list of 11 ways to see if you’ve thought of them already.
Method 1: Infinite Loop
This method is a silly method that will consume a CPU core idle.
Method 2: select{}
This is a common trick I use when writing examples, using a select statement without a case, with very few words, so I like to use it.
Method 3: Read or write data from a channel without cache
Both methods will be blocked.
Method 4: Read data from or write data to a nil channel
Both methods will be blocked.
Method 5: Mutual Exclusion Locks
Read and write locks are similar.
Method 6: WaitGroup
Perhaps you have mastered the laws, many synchronization primitives are available, such as Cond, semaphore, we will not repeat the introduction.
Method 7: Blocking I/O
The simplest one is to use os.Stdin
, but you can also use files, sockets, etc., as long as they are blocking I/O.
Method 8: Use signal.Notify
This is a trick I use a lot in my products to capture os.Signal
and achieve a graceful exit.
Method 9: Use the signal.Notify
variant
Use Context, you can pass the context to the subgoroutine, Context is also suitable for use with select.
Method 10: fmt.Scanln()
A more concise way of blocking I/O.
Method 11: runtime.Goexit()
The main program exits but the function does not return until the subgoroutine finishes.
Ref
https://colobu.com/2023/05/22/how-to-block-the-go-app-exit/