Channels versus Closure Variables

on

Reference: https://groups.google.com/forum/#!topic/golang-nuts/37ecI_PFFps

Fibonacci series using channel

//http://play.golang.org/p/XuJuDOt4kr

package main
import (
    "fmt"
    "strconv"
)
func fibonacci(n int, c chan int) {
    x, y := 0, 1
    for i := 0; i < n; i++ {
        c <- x
        x, y = y, x+y
    }
    close(c)
}
func fibo(n int) (result int) {
    x, y := 0, 1
    for i := 0; i < n; i++ {
        result = x
        x, y = y, x+y
    }
    //close(c)
    return result
}
//a function that returns a channel (like a promise object)
func fibo1(arg int) <-chan int {
    c := make(chan int)
    go func(i int) {
        c <- fibo(i)
    }(arg)
    return c
}
func main() {
    c := make(chan int, 10)
    go fibonacci(cap(c), c)
    sum := 0
    for i := range c {
        sum += i
        fmt.Println(i)
    }
    fmt.Println("Sum=" + strconv.Itoa(sum))
    sum = <-fibo1(21) + <-fibo1(14) + <-fibo1(7)
    fmt.Println("Sum of three fibo functions: " + strconv.Itoa(sum))
    fmt.Scanln()
}

Output:

0
1
1
2
3
5
8
13
21
34
Sum=88
Sum of three fibo functions: 7006

Fibonacci series using closure variable and WaitGroup

package main
import (
    "fmt"
    "strconv"
    "sync"
)
func fibonacci(n int, c chan int) {
    x, y := 0, 1
    for i := 0; i < n; i++ {
        c <- x
        x, y = y, x+y
    }
    close(c)
}
func fibo(n int) (result int) {
    x, y := 0, 1
    for i := 0; i < n; i++ {
        result = x
        x, y = y, x+y
    }
    //close(c)
    return result
}
// fib returns a function that returns
// successive Fibonacci numbers.
//http://golang.org/doc/play/fib.go
func fib() func() int {
    a, b := 0, 1
    return func() int {
        a, b = b, a+b
        return a
    }
}
func main() {
    c := make(chan int, 10)
    go fibonacci(cap(c), c)
    sum := 0
    for i := range c {
        sum += i
        fmt.Println(i)
    }
    fmt.Println("Sum=" + strconv.Itoa(sum))
    //fmt.Println(fibo(10))
    var wg sync.WaitGroup
    var f1, f2, f3 int
    wg.Add(3)

    go func(arg int) {
        defer wg.Done()
        f1 = fibo(arg)
        fmt.Println("Goroutine1: " + strconv.Itoa(f1))
    }(21)

    go func(arg int) {
        defer wg.Done()
        f2 = fibo(arg)
        fmt.Println("Goroutine2: " + strconv.Itoa(f2))
    }(14)

    go func(arg int) {
        defer wg.Done()
        f3 = fibo(arg)
        fmt.Println("Goroutine3: " + strconv.Itoa(f3))
    }(7)
    wg.Wait()
    sum = f1 + f2 + f3
    fmt.Println("Sum of three fibo functions: " + strconv.Itoa(sum))
    fmt.Scanln()
}
Output:

0
1
1
2
3
5
8
13
21
34
Sum=88
Goroutine1: 6765
Goroutine2: 233
Goroutine3: 8
Sum of three fibo functions: 7006





Advertisements

Subjectivity aside, leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s