go - How to Pass Console Input to Subprocess from Golang -
i have following c program.
#include<stdio.h> int main() { int = 0; char ch; printf("starting test application\n"); (i=0; i<10; i++) { ch = getchar(); printf("character @ %d = %d\n", i, ch); } return 0; } i want run program sub-process go language. in go code, have byte array want used input c program. tried following approach , did not work.
cmd := exec.command("/home/dodtech/go-workspace/machinelistener/test") cmd.stdout = os.stdout err := cmd.start() if err == nil { var ctrlc [9]byte ctrlc[0] = 0x00 ctrlc[1] = 0x01 ctrlc[2] = 0x02 ctrlc[3] = 0x03 ctrlc[4] = 0x04 ctrlc[5] = 0x05 ctrlc[6] = 0x06 ctrlc[7] = 0x07 ctrlc[8] = 0x08 ctrlc[9] = 0x09 cmd.stdin = bytes.newreader(ctrlc[0:]) cmd.stdin.read(ctrlc[0:]) } to noted, both of above program test program , represent want achieve. not meant efficient , not written according programming guidelines(for example way byte array created). if can solve problem small case, able port actual software code. appreciate if can guide me how can .
if want write sub-process standard input, should use stdinpipe.
like:
substdin, err := cmd.stdinpipe() // check err defer substdin.close() io.writestring(substdin, "hello world") see docs complete example:
Comments
Post a Comment