-
-
Save balaa/d6ec2f9a948bbe015048d30caf5c06c9 to your computer and use it in GitHub Desktop.
Revisions
-
t9md revised this gist
Feb 5, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ package main /* -
t9md revised this gist
Feb 5, 2015 . 1 changed file with 15 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ func fibonacci(out, quit chan int) { select { case out <- x: // if out-ch is available for write(means out-ch's buffer is not full), send next fib number x x, y = y, x+y case <-quit: // if any data come to quit-ch, then break inifinite loop by return. fmt.Println("quit") return } @@ -53,8 +53,8 @@ func main() { // Create anonymous func() and immediately execute as goroutine. // This function iterate 10 times of (read fib number from out-ch and Println). // After that, send `0` to quit-ch to notify other fibonacci() to break inifinit loop. // func() is closure referencing outer `out`, `quit` channel variable which originaly // created in enclosing main() function. go func() { for i := 0; i < 10; i++ { fmt.Println(<-out) // pop data from out-ch 10 times @@ -82,20 +82,20 @@ func main() { numbers, unless some data is sent to channel 'quit' Communication channel(out, quit) is passed by arguments. +-fibonacci(out, quit) in main()--+ +--- go func() as goroutine ------+ | | | | | x, y := 0, 1 | | | | for { | | | | select { | ------------ | for i := 0; i < 10; i++ { | *1 | case out <- x: x == out<int> ====> fmt.Println(<-out) //(*1) | groutine func() feel 10 times of data from out-ch | x, y = x, x+y | ------------ | } | as sufficient. | | | | after for loop finish... | | ------------ | | | case <- quit: <=== quit<int> <== 0 quit <- 0 (*2) | *2 | fmt.Println("quit") | ------------ | | Send 0 to quit-ch for the purpose of stopping | return | | | inifinite loop of fibonacci(). | } | | | | } | | | +--------------------------------+ +---------------------------------+ */ -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 46 additions and 47 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,27 +27,27 @@ func fibonacci(out, quit chan int) { func main() { /* main() start +-- main() --+ | | | | | | | | +------------+ */ out := make(chan int) quit := make(chan int) /* two channel (out, quit) created +-- main() --+ | |-------------- | | out<int> | |-------------- | |-------------- | | quit<int> | |-------------- +------------+ */ // Create anonymous func() and immediately execute as goroutine. @@ -57,47 +57,46 @@ func main() { // created in enclosing main() function go func() { for i := 0; i < 10; i++ { fmt.Println(<-out) // pop data from out-ch 10 times } quit <- 0 // then send 0 to quit }() /* +--- main () -------------+ +---- go func() as goroutine ----------+ | | | | | | ------------ | for i := 0; i < 10; i++ { | *1 | | out<int> ====> fmt.Println(<-out) //(*1) | Blocking here for now. | | ------------ | } | since no data come from out-ch. | | | | First data will come after fibonacci() start | | ------------ | | | | quit<int> <== 0 quit <- 0 | *2 | | ------------ | | Send 0 to quit-ch to break | | | | inifinite loop of fibonacci(). +-------------------------+ +--------------------------------------+ */ fibonacci(out, quit) /* Start fibonacci number generater fibonacci(), this fibonacci() function inifinitely generate fibonacci numbers, unless some data is sent to channel 'quit' Communication channel(out, quit) is passed by arguments. +-fibonaci(out, quit) in main()--+ +--- go func() as goroutine ------+ | x, y := 0, 1 | | | | for { | | | | select { | | | | case out <- x: | ------------ | for i := 0; i < 10; i++ { | *1 | x, y = x, x+y x == out<int> ====> fmt.Println(<-out) //(*1) | groutine func() feel 10 times of data from out-ch | | ------------ | } | as sufficient. | | | | after for loop finish... | | ------------ | | | case <- quit: <=== quit<int> <== 0 quit <- 0 (*2) | *2 | fmt.Println("quit) | ------------ | | Send 0 to quit-ch for the purpose of break | return | | | inifinite loop of fibonacci(). | } | | | | } | | | +--------------------------------+ +---------------------------------+ */ } -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ package main /* -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 46 additions and 45 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,27 +26,27 @@ func fibonacci(out, quit chan int) { func main() { /* main() start +-- main() --+ | | | | | | | | +------------+ */ out := make(chan int) quit := make(chan int) /* two channel (out, quit) created +-- main() --+ | |-------------- | | out<int> | |-------------- | |-------------- | | quit<int> | |-------------- +------------+ */ // Create anonymous func() and immediately execute as goroutine. @@ -62,40 +62,41 @@ func main() { }() /* +--- main () -------------+ +---- go func() as goroutine ----------+ | | | | | | ------------ | for i := 0; i < 10; i++ { | *1 | | out<int> ====> fmt.Println(<-out) //(*1) | Blocking here for now. | | ------------ | } | since no data come from out-ch. | | | | First data will come after fibonacci() start | | ------------ | | | | quit<int> <== 0 quit <- 0 | *2 | | ------------ | | Send 0 to quit-ch for the purpose of break | | | | inifinite loop of fibonacci(). +-------------------------+ +--------------------------------------+ */ fibonacci(out, quit) /* Start fibonacci number generater fibonacci(), this fibonacci() function inifinitely generate fibonacci communication channel(out, quit) is passed by arguments. numbers, unless some data is sent to channel 'quit' +-fibonaci(out, quit) in main()--+ +--- go func() as goroutine ------+ | x, y := 0, 1 | | | | for { | | | | select { | | | | case out <- x: | ------------ | for i := 0; i < 10; i++ { | *1 | x, y = x, x+y x == out<int> ====> fmt.Println(<-out) //(*1) | groutine func() feel 10 times of data from out-ch | | ------------ | } | as sufficient. | | | | after for loop finish... | | ------------ | | | case <- quit: <=== quit<int> <== 0 quit <- 0 (*2) | *2 | fmt.Println("quit) | ------------ | | Send 0 to quit-ch for the purpose of break | return | | | inifinite loop of fibonacci(). | } | | | | } | | | +--------------------------------+ +---------------------------------+ */ } -
t9md revised this gist
Feb 4, 2015 . No changes.There are no files selected for viewing
-
t9md revised this gist
Feb 4, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ package main /* -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,7 +59,6 @@ func main() { for i := 0; i < 10; i++ { fmt.Println(<-out) // pop data from chan-out 10 times } quit <- 0 // then send 0 to quit }() /* -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 32 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,4 @@ package main /* @@ -54,46 +53,51 @@ func main() { // Create anonymous func() and immediately execute as goroutine. // This function iterate 10 times of (read fib number from out-ch and Println). // After that, send `0` to quit-ch to notify other fibonacci() to break inifinit loop. // func() is closure referencing `out`, `quit` channel variable which originaly // created in enclosing main() function go func() { for i := 0; i < 10; i++ { fmt.Println(<-out) // pop data from chan-out 10 times } fmt.Println(str) quit <- 0 // then send 0 to quit }() /* +--- main () -------------+ +---- go func() as goroutine ----------+ | | | | | | ------------ | for i := 0; i < 10; i++ { | *1 | | out<int> ====> fmt.Println(<-out) //(*1) | Blocking here for now. | | ------------ | } | since no data come from out-ch. | | | | First data will come after fibonacci() start | | ------------ | | | | quit<int> <== 0 quit <- 0 | *2 | | ------------ | | Send 0 to quit-ch for the purpose of break | | | | inifinite loop of fibonacci(). +-------------------------+ +--------------------------------------+ */ fibonacci(out, quit) // communication channel is passed as function arguments. /* start fibonacci number generater fibonacci(), this fibonacci() function inifinitely generate fibonacci numbers, unless some data is sent to channel 'quit' +-fibonaci(out, quit) in main()--+ +--- go func() as goroutine ------+ | x, y := 0, 1 | | | | for { | | | | select { | | | | case out <- x: | ------------ | for i := 0; i < 10; i++ { | *1 | x, y = x, x+y x == out<int> ====> fmt.Println(<-out) //(*1) | groutine func() feel 10 times of data from out-ch | | ------------ | } | as sufficient. | | | | after for loop finish... | | ------------ | | | case <- quit: <=== quit<int> <== 0 quit <- 0 (*2) | *2 | fmt.Println("quit) | ------------ | | Send 0 to quit-ch for the purpose of break | return | | | inifinite loop of fibonacci(). | } | | | | } | | | +--------------------------------+ +---------------------------------+ */ } -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ package main /* -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 18 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ package main /* @@ -49,29 +50,30 @@ func main() { +------------+ */ // Create anonymous func() and immediately execute as goroutine. // This function iterate 10 times of (read fib number from out-ch and Println). // After that, send `0` to quit-ch to notify other fibonacci() to break inifinit loop. go func() { for i := 0; i < 10; i++ { fmt.Println(<-out) // pop data from chan-out 10 times } quit <- 0 // then send 0 to quit }() /* +--- main () -------------+ +--- goroutine: func() ---------------+ | | | | | | ------------ | for i := 0; i < 10; i++ { | *1 | | out<int> ====> fmt.Println(<-out) //(*1) | Blocking here for now. | | ------------ | } | since no data come from out-ch. | | | | First data will come after fibonacci() start | | ------------ | | | | quit<int> <== 0 quit <- 0 | *2 | | ------------ | | Send 0 to quit-ch for the purpose of break | | | | inifinit loop of fibonacci() | | | | +-------------------------+ +--------------------------------------+ */ fibonacci(out, quit) -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ func fibonacci(out, quit chan int) { x, y := 0, 1 for { select { case out <- x: // if out-ch is available for write(means out-ch's buffer is not full), send next fib number x x, y = y, x+y case <-quit: // if some data come to quit-ch, then break inifinite loop by return. fmt.Println("quit") -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,9 +15,9 @@ func fibonacci(out, quit chan int) { x, y := 0, 1 for { select { case out <- x: // if out-ch is available for write(mean empty), send next fib number x x, y = y, x+y case <-quit: // if some data come to quit-ch, then break inifinite loop by return. fmt.Println("quit") return } -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -65,7 +65,7 @@ func main() { | t.Println(<-out) //(*1) <== out<int> !! Blocking here for now. | |------------- !! since no data come from `out` channel. | | !! First data will come after fibonacci() start | } | | |------------- | quit <- 0 0 =======> quit<int> | |------------- -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -65,7 +65,7 @@ func main() { | t.Println(<-out) //(*1) <== out<int> !! Blocking here for now. | |------------- !! since no data come from `out` channel. | | !! First data will come after fibonacci() start | } | | |------------- | quit <- 0 0 =======> quit<int> | |------------- @@ -77,7 +77,7 @@ func main() { fibonacci(out, quit) /* fibonacci() start, this fibonacci() function inifinitely generate fibonacci numbers, unless some data is sent to channel 'quit' +---anonymous func() in main()--+ +-- fibonacci()---------+ | for i := 0; i < 10; i++ { |------------------- | select { | -
t9md revised this gist
Feb 4, 2015 . 1 changed file with 13 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -79,18 +79,18 @@ func main() { fibonacci() start, this fibonacci() function inifinitely generate fibonacci numbers, unless some data is send to channel 'quit' +---anonymous func() in main()--+ +-- fibonacci()---------+ | for i := 0; i < 10; i++ { |------------------- | select { | | t.Println(<-out) <====== out<int> <== x | case out <- x: | | } |------------------- | x, y = x, x+y | | | | | | | | | | |------------------- | | | quit <- 0 0 =======> quit<int> =======> case <- quit: | | |------------------- | fmt.Println("quit) | | | | return | | | | } | +-------------------------------+ +-----------------------+ */ } -
t9md created this gist
Feb 4, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,96 @@ package main /* Channel and select {} illustrated by t9md =========================================================== sample program to understand goroutine communication via channel. based on go-tour's example https://tour.golang.org/concurrency/5 */ import "fmt" func fibonacci(out, quit chan int) { x, y := 0, 1 for { select { case out <- x: // if x is available for write(mean empty), send next fib number x to out-ch x, y = y, x+y case <-quit: // if some data is stocked in quit-ch, then break inifinite loop by return. fmt.Println("quit") return } } } func main() { /* main() start +-- main() --+ | | | | | | | | +------------+ */ out := make(chan int) quit := make(chan int) /* two channel (out, quit) created +-- main() --+ | |-------------- | | out<int> | |-------------- | |-------------- | | quit<int> | |-------------- +------------+ */ // Create anonymous func() and immediately call // this iterate 10 times of (read fib number(int) from out-ch and Println). // After that, send `0` to quit-ch to notify other goroutine(=fibonacci() ) to break inifinit loop. go func() { for i := 0; i < 10; i++ { fmt.Println(<-out) // pop data from chan-c 10 times } quit <- 0 // then send 0 to quit }() /* +---anonymous func() in main()--+ | for i := 0; i < 10; i++ { |------------- !! (*1) | t.Println(<-out) //(*1) <== out<int> !! Blocking here for now. | |------------- !! since no data come from `out` channel. | | !! First data will come after fibonacci() start | } | !! | |------------- | quit <- 0 0 =======> quit<int> | |------------- | | | | +-------------------------------+ */ fibonacci(out, quit) /* fibonacci() start, this fibonacci() function inifinitely generate fibonacci numbers, unless some data is send to channel 'quit' +---anonymous func() in main()--+ +-- fibonacci()---------+ | for i := 0; i < 10; i++ { |------------------- | select { | | t.Println(<-out) <====== out<int> <== x | case out <- x: | | } |------------------- | x, y = x, x+y | | | | | | | | | | |------------------- | | | quit <- 0 0 =======> quit<int> =======> case <- quit: | | |------------------- | fmt.Println("quit) | | | | return | | | | } | +-------------------------------+ +-----------------------+ */ }