I am new to golang, so this deadlock issue is unknown for me. I've been Reading some articles, but it seems to be a quick fix in this program.
package main
import (
"fmt"
)
//ping
func addValue(input1 int, input2 int, chn2 chan<- int) {
chn2 <- input1 + input2
}
//pong
//function to write 1st user input to channel_1
func getUserInput(input1 int, input2 int, chn2 <-chan int, chn1 chan int, chn3 chan int) {
chn1 <- input1
chn1 <- input2
//receiving info from other func and read into this func.
val := <-chn2
chn3 <- val
}
//Main function
func main() {
var input1 int
var input2 int
chn1 := make(chan int, 3)
chn2 := make(chan int)
chn3 := make(chan int)
//taking inputs from terminal
fmt.Scanln(&input1)
fmt.Scan(&input2)
//calling go functions
go getUserInput(input1, input2, chn1, chn2, chn3)
go addValue(input1, input2, chn2)
//shifting values from channels to var.
/*x:=
y :=
z := <-chn3*/
//print out the values on the terminal
fmt.Println("Reading first input: ", <-chn1)
fmt.Println("Reading second input: ", <-chn1)
fmt.Println("Giving resulted value: ", <-chn3)
}
You had function argument order wrong. Just change the function header and you are good
Change
func getUserInput(input1 int, input2 int, chn2 <-chan int, chn1 chan int, chn3 chan int) {
to
func getUserInput(input1 int, input2 int, chn1 chan int,chn2 <-chan int, chn3 chan int) {
Play link :https://play.golang.com/p/3lKLoEytr9J
When you are calling the method getUserInput
you are passing the channels chn1 and chn2 in the wrong position. Please replace the method calls like this go getUserInput(input1, input2, chn2, chn1, chn3)
or switch the position in the method.