Passing info from channel to channel while altering it in go - go

I'm new to the go programming language.
I just learned about channels from their website, and tried to create
The following program:
1) I want to create 100,000 channels.
2) when the first channel receives a msg (value) he adds 1 to it
and pass it on to the next channel (by order).
3) when we'll get to the last channel it Will print 100,001.
I have created the channels with a loop, but I find it difficult to find
how to pass and alter the data in a "domino" fashion like I described.
Any solution or reference would be appreciated.
Thanks!
Here is my code:
package main
func addOneAndPass(c1 chan int, c2 chan int) {
c := make(chan int)
c <- 1
val := <- c
}
func main() {
const n = 100000
var channels [n]chan int
for i := 0; i < n; i++ {
channels[i] = make(chan int)
}
}

https://play.golang.org/p/ku-Dretm8EA
package main
import (
"fmt"
)
func add1(in chan int) (chan int) {
i := <-in
out := make(chan int, 1)
out <- (i+1)
return out
}
func main() {
ch := make(chan int, 1)
ch <- 1
for i := 0; i < 100000; i++ {
ch = add1(ch)
}
fmt.Println(<-ch)
}
another solution: https://play.golang.org/p/uWVxSG0xgqU
package main
import (
"fmt"
)
func add1(in, out chan int) {
i := <-in
out <- (i+1)
}
func main() {
start := make(chan int)
var in = start
var ch chan int
for i := 0; i < 100000; i++ {
ch = make(chan int)
go add1(in, ch)
in = ch
}
start <- 1
fmt.Println(<-ch)
}

Related

golang goroutines, chanel and sync.WaitGroup

I use simple code, but I get deadlock all the time.
Please explain to the beginner what I am doing wrong.
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
ok := make(chan int, 3)
for i := 0; i < 2; i++ {
wg.Add(1)
go func(i int, wg *sync.WaitGroup) {
for x := range ok {
fmt.Println(i, x)
if x >= 5 {
ok <- x - 1
}
}
wg.Done()
}(i, &wg)
}
ok <- 1
ok <- 3
ok <- 2
ok <- 5
ok <- 3
ok <- 9
wg.Wait()
}
fatal error: all goroutines are asleep - deadlock!
The background of the problem is that the range operator reads from the channel until the channel is closed. Hence the for-range loop keeps waiting for more input from the channel ok even though there are no more inputs to come at some point. At the same time wg.Wait() is waiting for the goroutines to finish. Hence a deadlock!
Either you'll have to close the ok channel at some point, where
there are no more incoming values to the channel ok.
Or you may use the worker pool just like,
package main
import (
"fmt"
"strconv"
"sync"
)
func worker(wg *sync.WaitGroup, ch chan string, i int) {
defer wg.Done()
ch <- "worker process value " + strconv.Itoa(i)
}
func workerMonitor(wg *sync.WaitGroup, ch chan string) {
wg.Wait()
close(ch)
}
func doWork(ch <-chan string, done chan<- bool) {
for i := range ch {
fmt.Println(i)
}
done <- true
}
func main() {
var wg sync.WaitGroup
ch := make(chan string)
var arr = [6]int{1, 3, 2, 5, 3, 9}
for i := 0; i < len(arr); i++ {
wg.Add(1)
if arr[i] >= 5 {
for j := arr[i]; j >= 5; j-- {
wg.Add(1)
go worker(&wg, ch, j-1)
}
}
go worker(&wg, ch, arr[i])
}
go workerMonitor(&wg, ch)
done := make(chan bool, 1)
go doWork(ch, done)
<-done
}
Play ground url

Golang Channels dealing with send != receive

Consider the toy example below. The code works perfectly but when you interchange the 2 lines marked as replace, there will be a deadlock. Is there a better way to deal with such a situation when you have different number of sends and receives?
package main
import "fmt"
import "strconv"
func main() {
a := make(chan string)
b := make(chan string)
go func() {
for i := 0; i < 2; i++ {
go func(i int) {
fmt.Println(<-a)
b <- strconv.Itoa(i) + "b" // replace
a <- strconv.Itoa(i) + "a" // replace
}(i)
}
}()
a <- "0"
for i := 0; i < 2; i++ {
fmt.Println(<-b)
}
}
EDIT: using a select statement, there's a chance that a gets picked up by the select and there's still no way to prevent a deadlock because the goroutines can't execute
package main
import "fmt"
import "strconv"
func main() {
a := make(chan string)
b := make(chan string)
c := make(chan bool)
cancel := make(chan bool)
go func() {
for i := 0; i < 2; i++ {
go func(i int) {
fmt.Println(<-a)
b <- strconv.Itoa(i) + "b" // replace
a <- strconv.Itoa(i) + "a" // replace
c <- true
}(i)
}
}()
go func() {
<-c
<-c
cancel <- true
}()
a <- "0"
loop:
for {
select {
case ain := <-a:
fmt.Println("select", ain)
case bin := <-b:
fmt.Println("select", bin)
case <-cancel:
break loop
}
}
}
Use select:
package main
import "fmt"
import "strconv"
func main() {
a := make(chan string)
b := make(chan string)
go func() {
for i := 0; i < 2; i++ {
go func(i int) {
fmt.Println(<-a)
b <- strconv.Itoa(i) + "b" // replace
a <- strconv.Itoa(i) + "a" // replace
}(i)
}
}()
// regardless of which comes in first, this will handle it
select {
case ain <- a:
fmt.Println("sent a", ain)
case bin <- b:
fmt.Println("sent b", bin)
case <- cancel:
break
}
}
That example will sit and block for an item sent on either a or b channels.
Optionally I usually set a cancel token or a timeout.
Your original code deadlocks on the switch because you sent on B, where you were only listening on A. Golang requires you to be listening on the channel BEFORE you ever send to it. This is the pattern for multiple channels, not knowing which you are going to get first.

how to use gdb debug golang code to see what's inside channel?

i have this code for example
http://play.golang.org/p/9U22NfrXeq
// A concurrent prime sieve
package main
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
}
// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func Filter(in <-chan int, out chan<- int, prime int) {
for {
i := <-in // Receive value from 'in'.
if i%prime != 0 {
out <- i // Send 'i' to 'out'.
}
}
}
// The prime sieve: Daisy-chain Filter processes.
func main() {
ch := make(chan int) // Create a new channel.
go Generate(ch) // Launch Generate goroutine.
for i := 0; i < 10; i++ {
prime := <-ch
print(prime, "\n")
ch1 := make(chan int)
go Filter(ch, ch1, prime)
ch = ch1
}
}
How to see what's inside channel?
For example I want to debug this line:
prime := <-ch
By typing 'p ch' in gdb only prints address of the channel. How can I get contents?
You just have to dereference ch. With a very small program:
package main
func main() {
ch := make(chan int, 10)
ch <- 1
ch <- 2
ch <- 4
<-ch
}
Debugging:
(gdb) p *ch
$1 = struct hchan<int> = {1, 2, 4}

What is the correct way to write a distinct channel in Go?

I am a beginner in go.
I am trying to figure out an easy way to implement a channel that only output distinct values.
What I want to do is this:
package example
import (
"fmt"
"testing"
)
func TestShouldReturnDistinctValues(t *testing.T) {
var c := make([]chan int)
c <- 1
c <- 1
c <- 2
c <- 2
c <- 3
for e := range c {
// only print 1, 2 and 3.
fmt.println(e)
}
}
Should I be concern about memory leak here if I were to use a map to remember previous values?
You really can't do that, you'd have to keep a track of the values somehow, a map[int]struct{} is probably the most memory efficient way.
A simple example:
func UniqueGen(min, max int) <-chan int {
m := make(map[int]struct{}, max-min)
ch := make(chan int)
go func() {
for i := 0; i < 1000; i++ {
v := min + rand.Intn(max)
if _, ok := m[v]; !ok {
ch <- v
m[v] = struct{}{}
}
}
close(ch)
}()
return ch
}
I have done similar things before, except my problem was output inputs in ascending order. You can do this by adding a middle go routine. Here is an example:
package main
func main() {
input, output := distinct()
go func() {
input <- 1
input <- 1
input <- 2
input <- 2
input <- 3
close(input)
}()
for i := range output {
println(i)
}
}
func distinct() (input chan int, output chan int) {
input = make(chan int)
output = make(chan int)
go func() {
set := make(map[int]struct{})
for i := range input {
if _, ok := set[i]; !ok {
set[i] = struct{}{}
output <- i
}
}
close(output)
}()
return
}

Concurrent routines in Go

I want to write three concurrent routines that sends integer to each other. Now, I have implemented two concurrent routines which sends integers to each other.
package main
import "rand"
func Routine1(commands chan int, responses chan int) {
for i := 0; i < 10; i++ {
i := rand.Intn(100)
commands <- i
print(<-responses, " 1st\n");
}
close(commands)
}
func Routine2(commands chan int, responses chan int) {
for i := 0; i < 1000; i++ {
x, open := <-commands
if !open {
return;
}
print(x , " 2nd\n");
y := rand.Intn(100)
responses <- y
}
}
func main()
{
commands := make(chan int)
responses := make(chan int)
go Routine1(commands, responses)
Routine2(commands, responses)
}
However, when I want to add another routine which wants to send and receive integers to/from the above routines, it gives errors like "throw: all goroutines are asleep - deadlock!". Below is my code:
package main
import "rand"
func Routine1(commands chan int, responses chan int, command chan int, response chan int ) {
for i := 0; i < 10; i++ {
i := rand.Intn(100)
commands <- i
command <- i
print(<-responses, " 12st\n");
print(<-response, " 13st\n");
}
close(commands)
}
func Routine2(commands chan int, responses chan int) {
for i := 0; i < 1000; i++ {
x, open := <-commands
if !open {
return;
}
print(x , " 2nd\n");
y := rand.Intn(100)
responses <- y
}
}
func Routine3(command chan int, response chan int) {
for i := 0; i < 1000; i++ {
x, open := <-command
if !open {
return;
}
print(x , " 3nd\n");
y := rand.Intn(100)
response <- y
}
}
func main() {
commands := make(chan int)
responses := make(chan int)
command := make(chan int)
response := make(chan int)
go Routine1(commands, responses,command, response )
Routine2(commands, responses)
Routine3(command, response)
}
Can anybody help me, where is my mistake ? And can anybody help me, is it possible to create bidirectional channel or is it possible to create a common channel for int, string etc ?
You haven't declared the command and response variables in the main function.
func main() {
commands := make(chan int)
responses := make(chan int)
go Routine1(commands, responses, command, response)
Routine2(commands, responses)
Routine3(command, response)
}

Resources