Hello,
I’m coming now from the JAVA world, I want to hear your opinion on this.
I’m doing a small API-RESTful, I’m using Echo to not get out of scratch.
I have a third party program that to run it, I squeeze a command like this “program.exe --verbose”, with that, this program works on port 8080, it is also an API-RESTful. I need to run this program from within my API (Go-Echo), so whenever my API is run, that program will also run in parallel, so I tried that and it worked, but I do not know if I’m on the right track.
go func() {
cmd := exec.Command(“C:/go-workspace/ext-prog.exe”, “–verbose”)
out, err := cmd.CombinedOutput()
if err != nil {
rlog.Error(err)
} rlog.Info(string(out))
}()
This runs the program in the background, yes, waiting for it in a goroutine and printing the output. You might want to return early in the error case but otherwise the code is fine.
If you need to interact with the program as well you need some more code, of course.
The output is in the return from CombinedOutput. If the program cannot be run or returns a nonzero exit code you’ll also get an error from the same call. You might want to consider if the program doesn’t exit (i.e., waits for input), depending on what the program is. Or if you are running it more often than the time it takes to complete.