"Let's go Golang" let Xie Cheng kill himself

Posted by jstone3503 on Thu, 23 Dec 2021 09:44:13 +0100

"Let's go Golang" let Xie Cheng kill himself

In this blog post, we discuss Go's cooperation process and kill his own cooperation process. Here we need to use runtime Goexit().

Let's start with the runtime Goexit() and its usage

package main

import (
	"fmt"
	"runtime"
	"time"
)

func task051()  {
	defer fmt.Println("Here you go")

	fmt.Println("Song Xiang Tiange")
	fmt.Println("White hair floating green water")
	//Kill the current process
	// Goexit terminates the goroutine that calls it. No other goroutine is affected.
	// Goexit runs all deferred calls before terminating the goroutine. Because Goexit
	// is not a panic, any recover calls in those deferred functions will return nil.
	//
	// Calling Goexit from the main goroutine terminates that goroutine
	// without func main returning. Since func main has not returned,
	// the program continues execution of other goroutines.
	// If all other goroutines exit, the program crashes.
	runtime.Goexit()

	fmt.Println("Red palm clear wave")
}

func main() {
	go func() {
		fmt.Println("Goose goose goose")
		task051()
		fmt.Println("-lo bingwang")
	}()
	// Sleep pauses the current goroutine for at least the duration d.
	// A negative or zero duration causes Sleep to return immediately.
	time.Sleep(time.Second)
}

If goexit kills its goroutine, other goroutines will not be affected. Goexit will call all delay functions before terminating goroutine, because goexit is not a panic, and any call recovery in these delay functions will return nil.
Calling Goexit from the main coroutine terminates the main coroutine without returning the main function func main. Since the main function func main does not return, the program will continue to execute other goroutines. If all other goroutines terminate, the program will crash.

In this code, the main function first opens up a collaborative process, first outputs the first poem of chanting the goose, and then enters the task function. The task function is executed in the subprocess.

The result of this code is

Goose goose goose
 Song Xiang Tiange
 White hair floating green water
 Here you go

Here, the "red palm clear wave" is not output because it is at runtime The statement after goexit (), when the coroutine has been killed by itself.

However, the delay function is executed. Goexit will call all delay functions before terminating goroutine, because goexit is not a panic, and any call recovery in these delay functions will return nil. So "bring it, you" won't output.

But the author's name "- Luo Binwang" why didn't you output it? Think about it.

Because the function task051() kills the current coroutine. The author's name "- Luo Binwang" can't be executed because Xie Cheng has been killed.

The one who killed in front was the sub process.

We said that the main cooperative process can't die before. Now let's kill the main cooperative process and see what happens!

After the main cooperative process is killed, all the sub cooperative processes will be disordered and sleepless.

Let's take a look at the normal end of the main cooperation process

func main() {
	go task061()
	//Sleep for 5 seconds
	time.Sleep(5 * time.Second)

	//runtime.Goexit()
}
func task061(){
	for{
		fmt.Println("Task in progress...")
		time.Sleep(time.Second)
	}
}

The operation result is:

Task in progress...
Task in progress...
Task in progress...
Task in progress...
Task in progress...

The main process slept for 5 seconds, and the sub process said "task in progress..." before sleeping for a second. Therefore, when the main process ended, the main process said five words "task in progress...".

Now let's kill the main process and see what happens!

The runtime of the above code block Goexit() is activated so that it can run.

Let's see the results.

Task in progress...
Task in progress...
...(Omit some in the middle)
Task in progress...

The blogger waited for half a minute. Zixie Cheng kept shouting "the task is in progress..." like a screaming bear child whose parents are not at home at night.

Topics: Python Java Javascript Go