Last active
December 11, 2015 09:08
-
-
Save ende76/4577628 to your computer and use it in GitHub Desktop.
(SOLVED) Minimal program to illustrate a possible misunderstanding of what os.exec() can and cannot do with external programs' output.
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 ( | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
) | |
type MyWriter struct{} | |
func (w *MyWriter) Write(p []byte) (n int, err error) { | |
fmt.Fprintf(os.Stdout, "MyWriter: %v", string(p)) | |
n = len(p) | |
return | |
} | |
func main() { | |
// sleepecho is a simple, executable bash script, with contents: | |
// #!/bin/bash | |
// sleep 1 && echo -n . && sleep 1 && echo -n . && sleep 1 && echo -n . && sleep 1 && echo . | |
myCommand := exec.Command("./sleepecho") | |
myCommand.Stdout = &MyWriter{} | |
if err := myCommand.Start(); err != nil { | |
log.Fatal(err) | |
} | |
defer func() { | |
if err := myCommand.Wait(); err != nil { | |
log.Fatal(err) | |
} | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment