Huangsha hundred battles wear gold armor, not broken Loulan end not also

Ancient poetry, touched the sand, shining, loulan when words like always let a person feel a kind of desert beyond the great mystery of representative, for the vast majority of people who live in the inland, probably there is no fate in this life to witness this magnificent natural landscape, but very coincidentally in today’s morning saw dusty scene or have a sheen, This is probably the first time in my life to feel the power of nature. However, in modern times, modern people do not have the romantic attitude towards yellow sand as ancient people do. Although yellow sand is spectacular, it is a mistake to place it in different places.

Go coroutines

To get a global idea of the go language coroutines, we must first understand a few basic concepts.

1. What is concurrency? What is parallelism? What’s the difference between concurrency and parallelism?

Concurrency refers to the ability to handle multiple tasks; Parallelism refers to the ability to handle multiple tasks simultaneously; Parallelism refers to a concept at a time, while parallelism refers to a concept at a time.

What is a process, what is a thread, and what is a coroutine?

Process is the encapsulation of run-time program, is the basic unit of system resource scheduling and allocation, and realizes the concurrency of operating system.

Thread is a sub-unit of the process, is the basic unit of CPU scheduling and allocation, used to ensure the real-time performance of the program, realize the process of internal concurrency, thread is the operating system scheduling can identify the smallest execution schedulable unit.

A coroutine is a function or method that runs concurrently with other functions or methods. A GO coroutine can be considered a lightweight thread, but the cost of creating a GO coroutine is small compared to a thread.

3. Start of coroutines

Func hello() {fmt.println (" hello world goroutine")} func main() {go hello() time.sleep (1 * time.second) Println("main function")} func numbers() {for I := 1; i <= 5; i++ { time.Sleep(250 * time.Millisecond) fmt.Printf("%d ", i) } } func alphabets() { for i := 'a'; i <= 'e'; i++ { time.Sleep(400 * time.Millisecond) fmt.Printf("%c ", i) } } func main() { go numbers() go alphabets() time.Sleep(3000 * time.Millisecond) fmt.Println("main terminated") }Copy the code

Channel is the channel

1. What is channel?

A channel can be thought of as a communication channel between GO coroutines, just as water in a pipe flows from one end to the other. Through a channel, data can be sent from one end and received from the other end

2. Channel declaration?

Channels are associated with one type of data, and channels can only transport that type of data, and it is illegal to transport other data.

Func main() {var a chan int if a == nil {FMT.Println("channel a is nil, Going to define it") a = make(chan int) ftt. Printf("Type of a is %T", a)}}Copy the code

3. Receiving and sending of data in the channel

Func hello(done chan bool) {fmt.println ("Hello world goroutine") done <- true } func main() { done := make(chan bool) go hello(done) <-done fmt.Println("main function") }Copy the code

4. One-way channel

A one-way channel is a declared channel limit that can only receive or send data, but not both

Func sendData(sendch chan< -int) {sendch < -10} func main() {sendch := make(chan< -int) go sendData(sendch) fmt.Println(<-sendch) }Copy the code

5. Channel traversal

// For example, func producer(CHNL chan int) {for I := 0; i < 10; i++ { chnl <- i } close(chnl) } func main() { ch := make(chan int) go producer(ch) for v := range ch { fmt.Println("Received ",v) } }Copy the code

6. Channel closing verification

V, ok := <-ch if! ok { break }Copy the code

summary

Coroutines the contents of this part is the key to master coroutines related use of channel, the two parts of knowledge can be said to be the go the core of the language, than any other language, go the the combination of coroutines and channel can use a mobile phone bosses conference catch phrase to describe: “fort”, is must to grasp the contents of this part.