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