Skip to content

Instantly share code, notes, and snippets.

@ende76
Last active December 11, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ende76/4577628 to your computer and use it in GitHub Desktop.
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.
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