Last active
August 15, 2024 16:18
-
-
Save olegpolukhin/3a4379a400c2c928f2d23059a78f1b82 to your computer and use it in GitHub Desktop.
run external Python script in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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