You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
701 B
40 lines
701 B
12 years ago
|
package main
|
||
|
|
||
12 years ago
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
)
|
||
12 years ago
|
|
||
|
func fibonacci(c, quit chan int) {
|
||
|
x, y := 0, 1
|
||
|
for {
|
||
|
select {
|
||
|
case c <- x:
|
||
|
x, y = y, x + y
|
||
|
case <- quit:
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
12 years ago
|
count := 10
|
||
|
if len(os.Args) > 1 {
|
||
|
var err error
|
||
|
if count, err = strconv.Atoi(os.Args[1]); err != nil {
|
||
|
fmt.Fprintln(os.Stderr, os.Args[1], "is not an int!")
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
}
|
||
12 years ago
|
c := make(chan int)
|
||
|
quit := make(chan int)
|
||
12 years ago
|
go func(count int) {
|
||
|
for i := 0; i < count; i++ {
|
||
12 years ago
|
fmt.Println(<-c)
|
||
|
}
|
||
|
quit <- 0
|
||
12 years ago
|
}(count)
|
||
12 years ago
|
fibonacci(c, quit)
|
||
|
}
|