6 ms·
This is timely as I was just fiddling around with some CLI and subprocess communication in Go as I learn the language. Is there anything like envoy for Go? Whil
by shmageggy 13y ago
This is timely as I was just fiddling around with some CLI and subprocess communication in Go as I learn the language. Is there anything like envoy for Go? While interacting with processes wasn't too tough (once I found the bufio package -- doh!) there's still a lot of boilerplate involved. That's partially unavoidable given the static nature of the language, but it would be nice to have a library that streamlined some common patterns.
- cmccabe 13y agoI think what Go's subprocess stuff needs most is better examples. There aren't a lot of examples out there yet. For example, I saw one guy on StackOverflow use a complicated Reader thread to print a subprocess' output to stdout, when all he had to do was set Command#Stdout to os.stdout. I hope that I'm not assuming too much here (maybe you really did need bufio for your code), but in a lot of cases you don't need it. Like if you want to supply stdin to a process, just use Command#Stdin = strings.NewReader("what I want to send to stdin"). For getting the command output, it's often enough to just call Command#CombinedOutput and cast to a string-- no bufio required unless you're doing something fancy. I wrote some utility code that might or might not be helpful: https://github.com/cmccabe/test2/blob/master/subprocess.go https://github.com/cmccabe/test2/blob/master/subprocess.go
- shmageggy 13y agoI definitely agree about the examples. There's really nothing out there except what you might run into on SO. In my case I wanted to programmatically communicate with an interactive command line app, so I ended up using StdinPipe and StdoutPipe along with a bufio.Writer and bufio.Scanner. This allowed me to easily send strings and read the lines that came back, which worked reasonably well. I agree that this would be overkill for simpler cases, in which your approach makes sense.
- krasin 13y agoDo you use os/exec [1] package or directly start processes with os.StartProcess? [1] http://golang.org/pkg/os/exec/ http://golang.org/pkg/os/exec/
- shmageggy 13y agoI used exec