Format and remove en/02.x.md spaces
This commit is contained in:
18
en/02.7.md
18
en/02.7.md
@@ -8,7 +8,7 @@ goroutines and concurrency are built into the core design of Go. They're similar
|
||||
|
||||
goroutines run on the thread manager at runtime in Go. We use the `go` keyword to create a new goroutine, which is a function at the underlying level ( ***main() is a goroutine*** ).
|
||||
```Go
|
||||
go hello(a, b, c)
|
||||
go hello(a, b, c)
|
||||
```
|
||||
Let's see an example.
|
||||
```Go
|
||||
@@ -56,14 +56,14 @@ Before Go 1.5,The scheduler only uses one thread to run all goroutines, which me
|
||||
|
||||
goroutines run in the same memory address space, so you have to maintain synchronization when you want to access shared memory. How do you communicate between different goroutines? Go uses a very good communication mechanism called `channel`. `channel` is like a two-way pipeline in Unix shells: use `channel` to send or receive data. The only data type that can be used in channels is the type `channel` and the keyword `chan`. Be aware that you have to use `make` to create a new `channel`.
|
||||
```Go
|
||||
ci := make(chan int)
|
||||
cs := make(chan string)
|
||||
cf := make(chan interface{})
|
||||
ci := make(chan int)
|
||||
cs := make(chan string)
|
||||
cf := make(chan interface{})
|
||||
```
|
||||
channel uses the operator `<-` to send or receive data.
|
||||
```Go
|
||||
ch <- v // send v to channel ch.
|
||||
v := <-ch // receive data from ch, and assign to v
|
||||
ch <- v // send v to channel ch.
|
||||
v := <-ch // receive data from ch, and assign to v
|
||||
```
|
||||
Let's see more examples.
|
||||
```Go
|
||||
@@ -97,10 +97,10 @@ Sending and receiving data in channels blocks by default, so it's much easier to
|
||||
|
||||
I introduced non-buffered channels above. Go also has buffered channels that can store more than a single element. For example, `ch := make(chan bool, 4)`, here we create a channel that can store 4 boolean elements. So in this channel, we are able to send 4 elements into it without blocking, but the goroutine will be blocked when you try to send a fifth element and no goroutine receives it.
|
||||
```Go
|
||||
ch := make(chan type, n)
|
||||
ch := make(chan type, n)
|
||||
|
||||
n == 0 ! non-buffer(block)
|
||||
n > 0 ! buffer(non-block until n elements in the channel)
|
||||
n == 0 ! non-buffer(block)
|
||||
n > 0 ! buffer(non-block until n elements in the channel)
|
||||
```
|
||||
You can try the following code on your computer and change some values.
|
||||
```Go
|
||||
|
||||
Reference in New Issue
Block a user