Forked from olegpolukhin/Run external Python script in Golang
Created
September 3, 2022 16:12
-
-
Save tinhb92/399f9ba9c41fe84faa926315ae16ea21 to your computer and use it in GitHub Desktop.
run external Python script in Golang
This file contains hidden or 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