This is a creation in Article, where the information may have evolved or changed.
This article is to learn <<go language programming >>--Tsinghua University Press (Wang Peng authored) January 2014 first edition of some notes, if there is infringement, please inform the author, will be deleted within 24 hours, reproduced please indicate the source!
1. Goroutine
-Definition: A lightweight thread that is supported at the language level.
-All operating system invoke operations (including synchronous I/O operations) provided by the Go standard library will be given to the processor. So it's switching and managing not dependent on the system's processes and threads
-Is the function of the Go language library, not the functionality of the operating system. Goroutine is not implemented with threads. The essence is a piece of code, a function entry, and a stack assigned to it on the heap.
-Eliminates the overhead of creating and destroying threads frequently, creating millions of goroutine, but they are not directly scheduled by the operating system.
-Other languages generally support the process through the library, but in such a process calls a synchronous IO operation, such as network communication, local file read and write will block other concurrent execution of the process.
-Create method: With the go keyword in front of a function, this call is executed concurrently in a new goroutine until the function returns. (Note: Function return value will be discarded)
2. Channel
-Definition: Go uses the message mechanism as a means of communication between concurrent units.
-Each concurrent unit is self-contained, individual, and has its own variables. These variables cannot be shared between different concurrent units.
-Similar to pipe, you can use the channel to pass messages between two or more goroutine, and the channel is designed to ensure that only one goroutine can receive data from it at the same time. This avoids the problem of using mutex locks.
-The channel is sent and received in the original language operation, will not be interrupted, will only fail.
-Channel is the process of communication, inter-process communications generally use a distributed approach, such as: Socket or HTTP.
-Declaration and initialization: var channelname Chan ElementType elementtype Specifies the type of element that the channel can pass.
3. Receipt and transmission of data
-Use channel to pass data in different goroutine. Using the channel operation symbol: <-
-Send channelvar <-value
-Receive value: = <-ch
-writing data to the channel causes the program (which should be a goroutine bar) to block, knowing that other goroutine are reading the data from the channel.
-If the channel does not write data before, then reading the data from the channel will also block the program (it should be goroutine) until the data is written.
Package Mainimport ("FMT" "Math/rand") func Test (Ch Chanint){ //do not understand the results of why some end did not lose it?Fmt. Println ("Begin ...", CH) ch<-Rand. Int () fmt. Println ("End ...", CH)} Func Main () {CHS:= Make ([]chanint, 10) forI: = 0;i<10;i++{Chs[i]= Make (chanint) Go Test (Chs[i])} for_,ch: =Range chs{Value:= <-ch FMT. Println (Value)}} View Code