Go language is a modern and powerful concurrency programming language, and its goroutine is one of its most magical features. This article will explore the magic of Go language coroutines and demonstrate its powerful concurrency capabilities through specific code examples.
1. What is a coroutine?
Coroutine is a lightweight thread managed by the runtime environment of the Go language. Each Go coroutine runs in its own stack space, so the overhead of creating and destroying coroutines is very small. Through coroutines, concurrent execution can be easily achieved and the performance of the program can be improved.
2. Create a coroutine
In the Go language, use the keyword go
to create a coroutine. Here is a simple example:
package main import ( "fmt" "time" ) func hello() { fmt.Println("Hello, Go!") } func main() { go hello() time.Sleep(time.Second) }
In the above code, we define a hello
function and then use go hello in the
main function ()
Created a coroutine. The coroutines will be executed concurrently in the background and output "Hello, Go!". At the same time, use time.Sleep(time.Second)
in the main
function to pause the main thread in order to observe the output of the coroutine.
3. Communication between coroutines
Go language provides channel
to realize communication between coroutines. The following is a simple example:
package main import ( "fmt" ) func producer(ch chan int) { for i := 0; i < 5; i++ { ch <- i } close(ch) } func consumer(ch chan int) { for num := range ch { fmt.Println("Received:", num) } } func main() { ch := make(chan int) go producer(ch) go consumer(ch) for { if _, ok := <-ch; !ok { break } } }
In the above code, we define a producer
coroutine and a consumer
coroutine, and pass a channel
to communicate. producer
sends data to channel
, consumer
receives data from channel
and prints it. Finally, use the for
loop and close
in the main
function to correctly close the channel
to achieve synchronization of the coroutine.
4. Scheduling of coroutines
The runtime scheduler of the Go language will automatically schedule between the logical processor (Processor) and the coroutine to ensure fair scheduling and efficiency of the coroutine. Utilize computing resources. An example is as follows:
package main import ( "fmt" "runtime" ) func printNumbers() { for i := 1; i <= 5; i++ { fmt.Println("Number:", i) runtime.Gosched() } } func main() { runtime.GOMAXPROCS(2) // 设置逻辑处理器数量 go printNumbers() go printNumbers() fmt.Scanln() }
In the above code, we set the number of logical processors to 2 through runtime.GOMAXPROCS(2)
, and then create two coroutines to execute printNumbers at the same time
Function prints numbers. Use runtime.Gosched()
in the printNumbers
function to actively give up the processor and give another coroutine a chance to run.
5. Summary
Through the above code examples, we have deeply explored the magic of Go language coroutines. The lightweight, efficient, and concurrent execution capabilities of coroutines make Go language one of the preferred languages for developing concurrent programs. I hope this article can help readers better understand and utilize the advantages of Go language coroutines and improve the efficiency and quality of concurrent programming.
The above is the detailed content of Explore the magic of Go language coroutines. For more information, please follow other related articles on the PHP Chinese website!