I'm trying to have two golang programs communicating using pipes, each running concurrently e.g:
1 go run master/main/main.go
2 master/main/main.go calls slave.exe (built go program)
3 slave.exe prints out "Ping"
4 master/main/main.go reads "Ping" and writes "Pong"
5 slave.exe reads "Pong" and prints out "Message recieved: Pong"
The program reaches step 4 but it does not receive another message from slave.exe.
In master/main/main.go:
package main
import (
"fmt"
"os"
"os/exec"
"bufio"
)
func main() {
// Run compiled slave project:
c := exec.Command("main", insert_path_to_exe_here)
out, _ := c.StdoutPipe()
in, _ := c.StdinPipe()
c.Start() // Using Start() instead of Run() because Run() waits for program to finish before moving on.
inwriter := bufio.NewWriter(in)
outreader := bufio.NewReader(out)
// This should print "Ping".
fmt.Println(outreader.ReadString('
'))
inwriter.WriteString("Pong")
// This should print "Message received: Pong"
fmt.Println(outreader.ReadString('
'))
}
In slave/main/main.go:
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
fmt.Println("Ping")
reader := bufio.NewReader(os.Stdin)
s, _ := reader.ReadString('
')
fmt.Println("Message received: ", s)
}
To run:
- Build slave/main/main.go
- Replace insert_path_to_exe_here with the path to the built executable and replace "main" with the name of the executable.
- Run master/main/main.go