Pipeline (channel) summary in the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The channel is a more important part of the go language and is often used in concurrency in go. Try the following summary for the Go language pipeline today. The form of summary uses a question-and-answer approach to make the answers more purposeful.

Q1. What is a pipe?
Pipelines are goroutine communication between the go language at the language level * *, and we can use the channel to pass messages between multiple goroutine. Channel is * * in-process communication, is not support cross-process communication, if the need for interprocess communication, you can use the socket and other network methods.

The above is the concept of piping, below we look at the syntax of the pipeline.

Q2. The syntax of a pipeline?

The syntax for the entire go language is concise, and the pipeline is no exception, and its syntax is as follows:

It should be noted here that pipelines are type-dependent, that is, a pipe can only pass a value of one type. The data in the pipeline is FIFO .

1//declared in this elemtype refers to the type passed by this pipeline2 var channame chan Elemtype3//declares a pipeline that passes the type int4var ch chanint5//declare a map, the element is a bool type channel6 var m map[string] Chan bool7 8Define the syntax, define the need to use the built-in function make (), the following line of code is declaration +defining an integral pipe9CH: = Make (chanint)Ten//define the size of the pipe in advance, this line of code defines the size of the pipe OneCH: = Make (chanint, 100) A  -Read and write data in the pipeline, <-operator is a priority with the left-most Chan. -//write a data to the pipeline where it is important to note that writing data to a pipeline usually causes the program to block until a the//other goroutine read data from this pipeline -ch<-value -//read data, note: If there is no data in the pipeline, reading from the pipe causes the program to block until there is data -Value: = <-CH +  -//Unidirectional Piping +var ch1 chan<-float64//can only write float64 data to the inside, cannot read Avar CH2 <-chanint//only int type data can be read at  -//close the channel and call Close () directly - Close (CH) -//determines if CH is off, determines the value of OK, and if false, it is closed (the read is not blocked if it is closed) -X, OK: = <-ch

Q3. What are the piping usage scenarios?

In the first question, we already know that the pipeline can do interprocess communication, and go has its own support for the Association (keyword Go), and the pipeline is a way to communicate between the various processes. Here are a few simple examples to illustrate how pipelines are used in the process.

1 Package Main 2 import "FMT" 3  4func print () {  5     FMT. Println ("Hello World")  6}  7  8func main () {  9     for I: = 0; i < i++ {ten        go print ()    }  --}


The above code basically means: Use the parallel output 10 times "Hello World", but when you run the above code, you will find that there is no output. This is because although the GO keyword is used to create the co-process, but it has not yet waited for execution, the main function has exited, the process has been closed, so the association will not be executed.

If you have a C-related multi-threaded experience, you can already change the coprocessor to a thread, then call the thread's join method, and let the main thread wait for the child thread to finish executing before exiting. In the go language, we can take advantage of the pipeline's write blocking and read blocking to perform a similar thread join behavior. The code looks like this:

1 Package Main2Import "FMT"3 4Func Print (Ch chanint) {5Fmt. Println ("Hello World")6ch<-17 }8 9 Func Main () {TenCHS: = Make ([]chanint) OneFor I: = 0; I < 10; i++ { AChs[i] = Make (chanint) - go Print (chs[i]) -     } the      -For _, ch: =Range (CHS) { -<-CH -     } +}

With the above code, we can finish the parallel output 10 this Hello world effect.

There is a question for everyone, if you change print to

1 int ) {2     ch<-13     FMT. Println ("Hello World")4 }

What does the

print?

Because of the limited level, there will inevitably be errors, please correct me.
Thank you.
[3/30]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.