English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Go language provides a special feature called Goroutines. Goroutine is afunctionor method, can be executed independently and simultaneously with any other Goroutine existing in the program. In other words, each activity that executes simultaneously in Go language is called a Goroutine, and you can view Goroutine as a lightweight thread. Compared with threads, the cost of creating Goroutines is very small. Each program at least contains one Goroutine, and this Goroutine is calledMain Goroutine. If the main Goroutine terminates, all Goroutines running under the main Goroutine will also terminate; Goroutines always run in the background.
You can simply use the go keyword as a prefix for function or method calls to create your own Goroutine, as shown in the following syntax:
语法:
func name(){ // 语句 } // Use the go keyword before the function name go name()
package main import "fmt" func display(str string) { for w := 0; 6; w++ { fmt.Println(str) } } func main() { // 调用Goroutine go display("Welcome") //normal function call display("oldtoolbag.com) }
输出:
oldtoolbag.com oldtoolbag.com oldtoolbag.com oldtoolbag.com oldtoolbag.com oldtoolbag.com
In the above example, we only createddisplay()function, and then call this function in two different ways, the first is Goroutine, that is, go display("Welcome"), and the other is a regular function call, that is, display("w3codebox)
But you may have found a problem, it only displays the result of calling a normal function, but not the result of Goroutine, because when a new Goroutine is executed, the call to the Goroutine returns immediately. It does not wait for the Goroutine to complete execution like a normal function, but always proceeds to the next line after the call to the Goroutine and ignores the returned value of the Goroutine. Therefore, to correctly execute the Goroutine, we have made some changes to the program, as shown in the following code:
Modified Goroutine example:
package main import ( "fmt" "time" ) func display(str string) { for w := 0; 6; w++ { time.Sleep(1 * time.Second) fmt.Println(str) } } func main() { // 调用Goroutine go display("Welcome") //调用普通函数 display("w3codebox) }
输出:
欢迎 w3codebox w3codebox 欢迎 欢迎 w3codebox w3codebox 欢迎 欢迎 w3codebox w3codebox
我们在程序中添加了Sleep()方法,它使主Goroutine在新Goroutine执行的1秒之间睡眠1秒,在屏幕上显示欢迎,然后在1秒的主Goroutine重新调度并执行其操作后终止。这个过程一直持续到z的值<6,之后主Goroutine终止。在这里,Goroutine和普通函数同时工作。
Goroutine比线程开销小。
Goroutine存储在堆栈中,并且堆栈的大小可以根据程序的要求而增大和缩小。但是在线程中,堆栈的大小是固定的。
Goroutine可以使用通道进行通信,并且这些通道经过特殊设计,可以防止在使用Goroutines访问共享内存时出现争用情况。
假设一个程序有一个线程,并且该线程有许多与之关联的Goroutine。如果由于资源需求,任何Goroutine阻塞了线程,则所有其余Goroutine将分配给新创建的OS线程。所有这些细节对程序员都是隐藏的。
在Go语言中,您还可以为匿名函数启动Goroutine,换句话说,您可以简单地通过使用go关键字作为该函数的前缀来创建匿名Goroutine,如以下语法所示:
语法:
//匿名函数调用 go func (parameter_list) { // 语句 )(arguments)
package main import ( "fmt" "time" ) func main() { fmt.Println("欢迎!!到主函数") //创建匿名Goroutine go func() { fmt.Println("欢迎!!到oldtoolbag.com) }) time.Sleep(1 * time.Second) fmt.Println("再见!!到主函数") }
输出:
欢迎!!到主函数 欢迎!!到oldtoolbag.com 再见!!到主函数