Created
December 10, 2023 23:07
-
-
Save opentechnologist/e7047f5fbb518ede6e6a4d500128c08d to your computer and use it in GitHub Desktop.
An example of using goroutine channel synchronization and communication to emulate the yield control statement from popular programming languages.
This file contains 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 characters
/* | |
Copyright © 2013 Mario (BU) Flores Rey CC BY-NC-SA 4.0 DEED | |
License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | |
Tested on GO v1.2.2 | |
*/ | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
) | |
func YieldInt(fn func(ch chan int)) chan int { | |
ch := make(chan int) | |
go fn(ch) | |
return ch | |
} | |
func YieldRune(fn func(ch chan rune)) chan rune { | |
ch := make(chan rune) | |
go fn(ch) | |
return ch | |
} | |
func YieldString(fn func(ch chan string)) chan string { | |
ch := make(chan string) | |
go fn(ch) | |
return ch | |
} | |
func TestYieldSequentialInt() { | |
var yieldInt chan int | |
yieldInt = YieldInt(func(ch chan int) { | |
const start int = 100 | |
const end int = 1000 | |
const step int = 50 | |
time.Sleep(3 * time.Second) | |
for i := start; i <= end; i+=step { | |
ch <- i | |
time.Sleep(100 * time.Millisecond) | |
} | |
close(ch) | |
}) | |
for n := range yieldInt { | |
fmt.Printf("%d ", n) | |
} | |
fmt.Println() | |
} | |
func TestYieldRandomInt() { | |
var yieldInt chan int | |
yieldInt = YieldInt(func(ch chan int) { | |
const count int = 10 // constant number of integers to yield | |
time.Sleep(3 * time.Second) | |
for i := 0; i < count; i++ { | |
ch <- rand.Int() | |
time.Sleep(100 * time.Millisecond) | |
} | |
close(ch) | |
}) | |
for n := range yieldInt { | |
fmt.Printf("%d ", n) | |
} | |
fmt.Println() | |
} | |
func TestYieldRune() { | |
sentence := "the quick brown fox jumped over the lazy dog" | |
var yieldRune chan rune | |
yieldRune = YieldRune(func(ch chan rune) { | |
time.Sleep(3 * time.Second) | |
for _, c := range sentence { | |
ch <- c | |
time.Sleep(100 * time.Millisecond) | |
} | |
close(ch) | |
}) | |
for c := range yieldRune { | |
fmt.Printf("%c ", c) | |
} | |
fmt.Println() | |
} | |
func TestYieldString() { | |
words := []string { | |
"the", | |
"quick", | |
"brown", | |
"fox", | |
"jumped", | |
"over", | |
"the", | |
"lazy", | |
"dog", | |
} | |
var yieldString chan string | |
yieldString = YieldString(func(ch chan string) { | |
time.Sleep(3 * time.Second) | |
for _, s := range words { | |
ch <- s | |
time.Sleep(100 * time.Millisecond) | |
} | |
close(ch) | |
}) | |
for s := range yieldString { | |
fmt.Printf("%s ", s) | |
} | |
} | |
func main() { | |
TestYieldSequentialInt() | |
TestYieldRandomInt() | |
TestYieldRune() | |
TestYieldString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment