This example describes the go lightweight thread goroutine usage. Share to everyone for your reference. Specifically as follows:
Goroutine is a lightweight thread that is managed by the Go runtime environment.
Go f (x, Y, z)
To open a new goroutine execution
f (x, Y, z)
F,x,y and z are defined in the current goroutine, but run F in the new goroutine.
Goroutine is running in the same address space, so access to shared memory must be synchronized. Sync offers this possibility, but it's not always used in go, because there are other ways. (later in the content will be involved.) )
Copy Code code as follows:
Package Main
Import (
"FMT"
"Runtime"
)
Func say (s string) {
For I: = 0; I < 5; i++ {
Runtime. Gosched ()
Fmt. PRINTLN (s)
}
}
Func Main () {
Go say (' World ')
Say ("Hello")
}
I hope this article will help you with your go language program.