This is a creation in Article, where the information may have evolved or changed.
10. Notes Go language-concurrency
Goroutine
Goroutine is a lightweight thread that is managed by the GO runtime environment.
Go f (x, Y, z)
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 run in the same address space, so access to shared memory must be synchronized. Sync provides this possibility, but it's not often used in go, because there are other ways.
Package Main
Import (
"FMT"
"Time"
)
Func say (s string) {
For i:= 0; I < 5; i++ {
Time. Sleep (100* Time.millisecond)
Fmt. PRINTLN (s)
}
}
Func Main () {
Gosay ("World")
Say ("Hello")
}
Channel
Channel is a type of pipe that can be sent or received with the channel operator <-.
CH <-V//V is fed into channel ch.
V: = <-ch//is received from CH and assigned to V.
(the arrow is the direction of the data flow.) )
Like map and slice, the channel must be created before it is used:
CH: = make (chan int)
By default, both send and receive are blocked until the other end is ready. This allows the goroutine to synchronize without a definite lock or race variable.
Package Main
Import "FMT"
Func sum (a []int, C Chan int) {
sum:= 0
For_, V: = Range a {
sum+= V
}
c<-sum// and fed into C
}
Func Main () {
A: =[]int{7, 2, 8,-9, 4, 0}
C: =make (chan int)
Gosum (A[:len (a)/2], C)
Gosum (A[len (a)/2:], C)
x, y:= <-c, <-c// from C obtained in
Fmt. Println (x, y, X+y)
}
Perform:
-517 12
Buffering Channel
The channel can be _ with buffer. Give make a second parameter as the buffer length to initialize a buffered channel:
CH: = Make (chan int, 100)
When data is sent to the buffer channel, it is blocked only when the buffer is full. Accepts blocking when the buffer is emptied.
Modify the example so that the buffer is filled and then see what happens.
Package Main
Import "FMT"
Func Main () {
C: =make (chan int, 2)
c<-1
c<-2
Fmt. Println (<-C)
Fmt. Println (<-C)
}
Perform:
1
2
Range and close
The sender can close a channel to indicate that no value will be sent. The receiver can test whether the channel is closed by the second parameter of the assignment statement: when no value can be received and the channel has been closed, then V, OK: = <-ch
The OK will then be set to ' false '.
The loop ' for I: = Range C ' will continue to receive values from the channel until it is closed.
Note: Only the sender can close the channel, not the receiver. Sending data to a channel that has been closed can cause panic. Also note that the channel is different from the file, and typically you do not need to close them. Shutting down is necessary only if you need to tell the receiver that there is no more data, such as breaking a ' range '.
Package Main
Import (
"FMT"
)
Func Fibonacci (n int, c Chan int) {
X, y:= 0, 1
For i:= 0; I < n; i++ {
c<-x
X, y = y, x+y
}
Close (c)
}
Func Main () {
C: =make (chan int, 10)
Gofibonacci (Cap (c), c)
For i:= range C {
Fmt. Println (i)
}
}
Perform:
0
1
1
2
3
5
8
13
21st
34
Select
The SELECT statement allows a goroutine to wait on multiple communication operations.
Select blocks until one of the conditional branches can continue execution, and the conditional branch is executed. When more than one is ready, one is randomly selected.
Package Main
Import "FMT"
Func Fibonacci (c, quit Chan int) {
X, y:= 0, 1
for {
select{
Casec <-x:
X, y = y, x+y
Case<-quit:
Fmt. Println ("Quit")
Return
}
}
}
Func Main () {
C: =make (chan int)
quit:= make (chan int)
Gofunc () {
Fori: = 0; I < 10; i++ {
Fmt. Println (<-C)
}
quit<-0
}()
Fibonacci (C,quit)
}
Perform:
0
1
1
2
3
5
8
13
21st
34
Quit
Default selection
When the other conditional branches in Select are not ready, the ' default ' branch is executed.
For non-blocking send or receive, you can use the default branch:
Select {
case I: = <-c:
Use I
Default
Reading from C will block
}
Package Main
Import (
"FMT"
"Time"
)
Func Main () {
tick:= time. Tick (+ * time.millisecond)
boom:= time. After ($ * time.millisecond)
for {
select{
Case<-tick:
Fmt. Println ("tick.")
Case<-boom:
Fmt. Println ("boom!")
Return
Default
Fmt. Println (".")
Time. Sleep (50* Time.millisecond)
}
}
}
Perform:
.
.
Tick.
.
.
Tick.
.
.
Tick.
.
.
Tick.
.
.
boom!
End of entry
Go The documentation is an excellent start. It contains references, guides, videos, and more.
Learn how to organize your go code and work on it, see This video , or read How to write a go code .
Refer to the package manual If you need help with the standard library. The language itself helps, reading the language Specification is a pleasant thing.
Further exploring the concurrency model for go, see go concurrency model ( slideshow ) and go to concurrency model ( slideshow ) and read uses communication to share the memory of the Code journey.
To start writing a web app, see a simple programming environment ( slideshow ) and read the guide to writing web apps .
GO the class-a civic function in shows interesting function types.
Go Blog has a lot of information about go.
Mikespook Blog has a large number of Chinese-related articles and translations of Go.
Open Source ebook go Web programming and go Getting Started guides can help you learn more about the go language.
Visit golang.org for more information.
For any comments or suggestions regarding this project (Chinese), please submit issues here .