Skip to content

Instantly share code, notes, and snippets.

@olegpolukhin
Last active August 15, 2024 16:18
Show Gist options
  • Save olegpolukhin/3a4379a400c2c928f2d23059a78f1b82 to your computer and use it in GitHub Desktop.
Save olegpolukhin/3a4379a400c2c928f2d23059a78f1b82 to your computer and use it in GitHub Desktop.
run external Python script in Golang
package main
import (
"bufio"
"fmt"
"io"
"os/exec"
)
func main() {
cmd := exec.Command("python", "script.py", "--input-file", "documents/doc.png")
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
panic(err)
}
go copyOutput(stdout)
go copyOutput(stderr)
cmd.Wait()
}
func copyOutput(r io.Reader) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment