Go Routine - With external program in C ++

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))
}()

Could you tell me your opinion on the above code?

Sorry for the weak English.

Thank you.
Marcelo

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.

Exact Calmh,

This third-party program generates output on the console, I need to know how to get this output and generate log to file.

Another question, if this program fails it can interrupt my API? Is there any risk?

Thank you very much for your support.

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.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.