Goroutine of the Go language

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

In the go world, each concurrent activity becomes goroutine.

By creating Goroutine, it is very convenient to implement parallel operations.

If there is a function f (), then:

F (): Call function f () and wait for it to return

Go f (): Create a new goroutine that calls F () without waiting for


Go Language Program:

FIB Project Main.gopackage mainimport ("FMT" "Time") Func main () {Go spinner (. millisecond) Const N = 46fibN: = FIB (n)//SLOWFMT. Printf ("\rfibonacci (%d) =%d\n", N, fibn)}func spinner (delay time.) Duration) {for {for I, r: = Range ' ABCD ' {fmt. Printf ("%d:%c\n", I, R) time. Sleep (delay)}}}func fib (x Int64) Int64 {if x < 2 {return X}return fib (x-2) + fib (x-1)}


Operation Result:

0:a1:b2:c3:d0:a1:b2:c3:d0:a1:b2:c3:d0:a1:b2:c3:d0:a1:b2:c3:d0:a1:b2:c3:d0:afibonacci (46) = 183631 1903

Program Description:

1. Function spinner () is a dead loop that loops through a character in a string and then goes to sleep for 1 seconds

2. The function fib () is a recursive algorithm of the computational program, very slow, especially in the investigation of the time into the parameter is 46, the actual operation is actually about 25 seconds

3. When the function main () ends, it forces the end of all goroutine and then exits the program


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.