Exemplo n.º 1
0
func main() {
	w := style.NewWriter()
	fmt.Fprintln(w, "calling RecursiveChannels in main")

	for received := range RecursiveChannels(0) {
		fmt.Fprintf(w, "received %v from c in main\n", received)
	}
}
Exemplo n.º 2
0
func main() {
	w := style.NewWriter()
	fmt.Fprintln(w, "calling RecursiveChannels in main")

	channel := RecursiveChannels(0)
	received := <-channel
	fmt.Fprintf(w, "received %v from c in main\n", received)
}
Exemplo n.º 3
0
func main() {
	w := style.NewWriter()
	w.SetDelay(100 * time.Millisecond)
	fmt.Fprintln(w, "calling RecursiveChannels in main")

	for received := range RecursiveChannels(0) {
		fmt.Fprintf(w, "received %v from c in main\n", received)
	}
}
Exemplo n.º 4
0
func main() {
	c := make(chan int)
	w := style.NewWriter()
	w.SetDelay(100 * time.Millisecond)
	go doSending(c)

	fmt.Fprintln(w, "receiving from c")
	value := <-c
	fmt.Fprintf(w, "received %v from c in main\n", value)
}
Exemplo n.º 5
0
func doSending(c chan int, recursionlevel int) {
	w := style.NewWriter(style.Bright, style.XTerm256FG(recursionlevel+1))
	w.SetDelay(100 * time.Millisecond)

	if recursionlevel == 10 {
		fmt.Fprintf(w, "sending to c at recursion level %v\n", recursionlevel)
		c <- recursionlevel
	} else {
		fmt.Fprintf(w, "recursing and receiving from channel at recursion level %v\n", recursionlevel)
		doSending(c, recursionlevel+1)
		received := <-c
		fmt.Fprintf(w, "received value %v from channel at recursion level %v, now sending back my level\n", received, recursionlevel)
		c <- recursionlevel
	}
}
Exemplo n.º 6
0
func RecursiveChannels(recursionlevel int) <-chan int {
	w := style.NewWriter(style.Bright, style.XTerm256FG(recursionlevel+1))

	returnChan := make(chan int)

	go func() {
		if recursionlevel < 10 {
			fmt.Fprintf(w, "recursing and receiving from channel at recursion level %v\n", recursionlevel)
			channel := RecursiveChannels(recursionlevel + 1)
			received := <-channel
			fmt.Fprintf(w, "received value %v from channel at recursion level %v, now sending back my level\n", received, recursionlevel)
		} else {
			fmt.Fprintf(w, "end of recursion: sending to channel at recursion level %v\n", recursionlevel)
		}
		returnChan <- recursionlevel
	}()

	return returnChan
}
Exemplo n.º 7
0
func RecursiveChannels(recursionlevel int) <-chan int {
	w := style.NewWriter(style.Bright, style.XTerm256FG(recursionlevel+1))
	w.SetDelay(100 * time.Millisecond)

	returnChan := make(chan int)

	if recursionlevel == 10 {
		fmt.Fprintf(w, "sending to channel at recursion level %v\n", recursionlevel)
		returnChan <- 1
		return returnChan
	}

	fmt.Fprintf(w, "recursing and receiving from channel at recursion level %v\n", recursionlevel)
	channel := RecursiveChannels(recursionlevel + 1)
	received := <-channel
	fmt.Fprintf(w, "received value %v from channel at recursion level %v, now sending back my level\n", received, recursionlevel)

	return returnChan
}
Exemplo n.º 8
0
func RecursiveChannels(recursionlevel int) <-chan int {
	w := style.NewWriter(style.Bright, style.XTerm256FG(recursionlevel+1))
	w.SetDelay(100 * time.Millisecond)

	returnChan := make(chan int)

	go func() {
		defer close(returnChan)
		if recursionlevel < 10 {
			fmt.Fprintf(w, "recursing and receiving from channel at recursion level %v\n", recursionlevel)
			for received := range RecursiveChannels(recursionlevel + 1) {
				fmt.Fprintf(w, "received value %v from channel at recursion level %v, now sending back my level\n", received, recursionlevel)
			}
		} else {
			fmt.Fprintf(w, "end of recursion: sending to channel at recursion level %v\n", recursionlevel)
		}
		returnChan <- recursionlevel
	}()

	return returnChan
}
Exemplo n.º 9
0
func doSending(c chan<- int) {
	w := style.NewWriter(style.Bright, style.Cyan)
	w.SetDelay(100 * time.Millisecond)
	fmt.Fprintln(w, "sending to c")
	c <- 1
}