On GO statement

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Go statements and channel types are the ultimate embodiment of the concurrent programming philosophy of the Go language. In contrast, go statements are much simpler to use than channels. As with the defer statement, the GO statement can also carry an expression statement. Note that execution of the GO statement ends quickly and does not cause blocking or noticeable delays in the current process. A simple example is as follows:


Go FMT. Println ("go!")
As you can see, the GO statement consists of only one keyword go and one expression statement. Similarly, the execution of the GO statement is not necessarily associated with the execution of the expression statements it carries. The only certainty here is that the latter will happen after the former is complete. When the GO statement is executed, the function it carries (also known as the Go function) and the parameters to be passed to it (if any) are encapsulated into an entity (i.e., goroutine) and placed into the corresponding queue to be run. The run-time system of the go language will remove the goroutine from the queue and perform the corresponding function call operation in a timely manner. Note that the evaluation of those parameters passed to the function here is performed when the GO statement is executed. This is similar to the defer statement.

Because of the uncertainty of the execution time of the Go function, the Go language provides many ways to help coordinate their execution. One of the simplest and most brutal methods is to call time. The Sleep function. Take a look at the following example:


Package Main


Import (
"FMT"
)


Func Main () {
Go FMT. Println ("go!")
}
When such a command source file is run, no content will appear on the standard output. Because the GO function is not yet waiting for the run-time system to execute, the main function is finished. The execution of the function main means that the execution of the entire program is complete. Therefore, the GO function has no chance of execution at all.

However, after the GO statement above, add a pair of time. After the call statement of the sleep function, the situation will be different:


Package Main


Import (
"FMT"
"Time"
)


Func Main () {
Go FMT. Println ("go!")
Time. Sleep (Time.millisecond)
}
Statement time. Sleep (Time.millisecond) Delays the execution end time of the main function by 100 milliseconds. 100 milliseconds is short, but enough go functions are scheduled to execute. The above command source file will be run as we would like to print out go! on the standard output.



Another gentleman's practice is to call runtime at the end of the main function. The gosched function. The corresponding program versions are as follows:


Package Main


Import (
"FMT"
"Runtime"
)


Func Main () {
Go FMT. Println ("go!")
Runtime. Gosched ()
}
Runtime. The purpose of the gosched function is to temporarily "break" the currently running Goroutine (the goroutine that runs the main function) and let the Go runtime system go to run other goroutine (here is with Go FMT. Println ("go!") The FMT is mapped and encapsulated. Println ("go!") of the Goroutine). As a result, the scheduling of several goroutine runs is more finely controlled.

There are, of course, other ways to meet these needs. And if you need to go around more goroutine, the next method might be more appropriate. Please look at the code:


Package Main


Import (
"FMT"
"Sync"
)


Func Main () {
VAR wg sync. Waitgroup
Wg. ADD (3)
Go func () {
Fmt. Println ("go!")
Wg. Done ()
}()
Go func () {
Fmt. Println ("go!")
Wg. Done ()
}()
Go func () {
Fmt. Println ("go!")
Wg. Done ()
}()
Wg. Wait ()
}

Sync. The Waitgroup type has three methods available for--add, done, and wait. Add adds a built-in integer to its owning value, and done causes that integer to be reduced by 1, while the wait method blocks the current goroutine (the Goroutine that runs the main function) until the integer is 0. Now you should understand the method used in the example above. Three goroutine are enabled in the main function to encapsulate three go functions. The WG is called at the end of each anonymous function. The Done method, and in this case the current go function executes immediately. When these three go functions have called the WG. After the done function, the WG at the end of the main function. The blocking effect of the Wait () statement disappears, and the execution of the main function ends immediately.


Package Mainimport (    "FMT") func main () {    ch1: = Do (chan int, 1)    CH2: = make (chan int, 1)    CH3: = Make (cha n int, 3)    go func () {        fmt. Println ("1")        ch1 <-1    } ()    go func () {        <-ch1        fmt. Println ("2")        CH2 <-2    } ()    go func () {        <-ch2        fmt. Println ("3")        CH3 <-3    } ()    <-ch3}


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.