Categories: Uncategorized

[Go]Output Fibonacci Series

Output Fibonaaci Series with Go.
package main

var ch = make(chan int)
var array [10]int

func fib(init int) {
    switch {
        case init == 0 || init == 1 :
            array[init] = 1
            ch <- array[init];
            return
        case init >= 2 :
            array[init] = array[init-2] + array[init-1]
            ch <- array[init]
            return
    }
}

func main() {
    for i := 0; i <= 0; i++ {
        array[i] = 0
    }

    for num := 0; num < 10; num++ {
        go fib(num);
        println(<- ch);
    }
}
Run.
$pwd
-> $GOPATH
$go install example/fib
$fib/fib
1
1
2
3
5
8
13
21
34
55
zuqqhi2