Esempio n. 1
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
	}
}
Esempio n. 2
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
}
Esempio n. 3
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
}
Esempio n. 4
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
}