This is a creation in Article, where the information may have evolved or changed.
co-process Goroutine Instead of being dispatched by the OS, the user layer frees the CPU itself to switch between the actuators. Go at the bottom of the help implementation involves system calls where the Go standard library assists in releasing the CPU in short, not through the OS to switch, self-switching, system operating expenses greatly reduced
Channel Channels The key to concurrent programming is the communication between the actuators, go through channel communication channel can be considered similar to other OS system in the message queue, but in go native support, so easy to use
What are the noteworthy areas of Message Queuing? Frequently asked questions include Create, close, or delete, block, timeout, priority, and so on, no exception in Golang. Listed below: Can the probe queue be full or empty? Or is it possible to try to read and write without blocking? What happens when read blocking and write blocking are turned off? Messages that are not read after closing are discarded? What happens when you send data or read data to a closed channel? How to detect the closing of a channel? Two places to read or write block the same channel, is there a priority? Can I set the time-out for blocking? How can it be bounced when blocked? Like some sort of signal? In fact, it is important to know the existence of these problems and to classify them, but it is not necessary to know the answers to these questions, because they are generally not too eccentric and can be used as a temporary test.
A known part of the answer: as if the blocking attempt to read and write close will cause the exit blocking (which seems to be a good feature) can detect that the close channel itself cannot set a timeout to understand these seems enough.
The difference is worth our attention, including: What are the special ways to read and write in addition to basic reading and writing? What's the difference between blocking, shutting down, and timing out? Found the recommended multi-channel read recommended synchronization method for select, range two keywords proposed time-out method
Select Select enables non-blocking multi-channel attempts to read and write, as well as blocking timeouts
var C, C1, C2, C3
Chanintvar i1, i2
int Select {
Case i1 = <-c1: //If you can walk through any case then take a random
print ( "received " , I1, "from c1\n" )
Case C2 <-i2:
print ( "Sent " , I2, "to c2\n" )
Case i3, OK: = (<-C3):
if OK {
print ( "received " , i3, "from c3\n" ) }
Else {
print ( "C3 is closed\n" ) }
default : //If case is blocked, go to default, if there is no default, block //default can not read or write any channel, so long as the default provides a way to not block, it is equivalent to implement a non-blocking attempt to read and write case
print ( "No communication\n" ) }
The way to implement a blocking timeout is to implement a timeout in case if you do not give the default way out
Timeout: = make (
ChanBOOL , 1)
Gofunc () { Time . Sleep (1e9) //This is waiting for 1 seconds timeout <-
true }() //Use the Timeout channel as a way out of the blocking timeout
Select {
case <-ch: //processing of data read from CH
case <-timeout: //If case is blocked, then 1 seconds will find a way out of here }
Range Range can read the translation of the Channelgo document in a For loop: For a channel, its iteration value is generated as a continuous value sent on that channel until the channel is closed. If the channel is nil, the range expression will block forever, and the range will block and can be unblocked by closing the channel.
Package main
Import ( "FMT" )
func Main ( ) { ch: = make
(
Chanint )
Gofunc () {
for I: = 0; I < i++ { Ch <-i }
}()
for w: =
Range CH { FMT. Println ( "FMT print" , W )
if W > 5 { //break//Here The break loop can also Close ( ch)
} } FMT. Println ( "after range or close ch!" ) }
There are other details about the concurrent programming of Golang, but the above is the main thread.
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.