The main function itself is also a Goroutine.
In Go language, the main function is the entry point of the program. When the program starts, the Go runtime creates a Goroutine named main and executes the main function in it. This main Goroutine is the main execution thread in the program, which is responsible for executing instructions and operations in the main function.
Similar to Goroutines created by other users, main Goroutine is also executed concurrently. It can be executed simultaneously with other Goroutines, taking advantage of the parallelism of multi-core processors.
The life cycle of main Goroutine is the same as that of a program. When the main function execution is completed or the function is explicitly called to terminate the program, the main Goroutine ends. Other executing Goroutines will also be terminated and the program will exit.
Therefore, we can regard the main function as a special Goroutine in the program, which is the entry and control center of the program. By starting other Goroutines inside the main function, we can implement concurrent execution of multiple tasks and make full use of the concurrency capabilities of the Go language.
In Go, blocking and non-blocking are used to describe the waiting behavior for an operation.
Blocking: When an operation is executed, if the operation cannot be completed immediately, the program will be blocked, that is, the execution will be paused until the operation is completed or a certain condition is met. In the blocking state, the program cannot continue to perform other tasks until the blocking operation is completed. For example, when we read a channel, if there is no readable data in the channel, the read operation will block until the data is readable.
package main import ( "fmt" "time" ) func main() { ch := make(chan int) // Create a channel go func() { (2 * ) ch <- 42 // Send data to the channel }() ("Waiting for data...") data := <-ch // Receive data from the channel. If there is no data in the channel, the row will block and wait ("Received data:", data) }
In the above example, we create a channelch
, and wait for 2 seconds in a Goroutine and send data to the channel (you can try to modify the numbers after the function and run them separately to see the effect). In the main Goroutine we try to get from the channelch
Receive data in. Since no data is available in the channel, the reception operation will be blocked until the data is sent to the channel.
Non-blocking: When an operation is executed, if the operation cannot be completed immediately, the program will not be blocked, but will return immediately, regardless of whether the operation is successful or completed. By using non-blocking operations, the program can continue to perform other tasks without waiting for the blocking operation to complete. For example, when reading a channel in a non-blocking manner, if there is no data in the channel to be read, the read operation will immediately return an error or default value.
package main import ( "fmt" "time" ) func main() { ch := make(chan int) // Create a channel go func() { (2 * ) ch <- 42 // Send data to the channel }() ("Waiting for data...") select { case data := <-ch: // Try to receive data from the channel ("Received data:", data) default: ("No data available") } }
In the above example, we useselect
Statement to try from the channelch
Receive data in. If there is no data available in the channel,select
The statement will be executed immediatelydefault
Branch, output "No data available" without blocking waiting. This way, the program can continue to perform other tasks without waiting for the data to arrive.
Blocking and non-blocking are concepts used to describe the waiting behavior for an operation. Blocking means that when an operation cannot be completed immediately, the program will pause execution until the operation is completed. A non-blocking means that when an operation cannot be completed immediately, the program will return immediately and continue to perform other tasks. In Go, channel operations are usually used to demonstrate the concepts of blocking and non-blocking.
This is the end of this article about blocking and non-blocking operation implementations in Go (Goroutine and main Goroutine). For more related Go blocking, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!