I would like to read an output command line per line. For this I use the StdoutPipe
method and the bufio
library:
package main
import (
"encoding/json"
"fmt"
"log"
"os/exec"
"bufio"
)
func main() {
cmd := exec.Command("printf", "{\"Name\": \"Bob\", \"Age\": 1}
%.0s", "{1..5}")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(stdout)
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
var person struct {
Name string
Age int
}
for scanner.Scan() {
if err := json.Unmarshal([]byte(scanner.Text()), &person); err != nil {
log.Fatal(err)
} else {
fmt.Printf("%s is %d years old
", person.Name, person.Age)
}
}
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
}
I should obtain 5 lines:
$ printf "{\"Name\": \"Bob\", \"Age\": 1}
%.0s" {1..5}
{"Name": "Bob", "Age": 1}
{"Name": "Bob", "Age": 1}
{"Name": "Bob", "Age": 1}
{"Name": "Bob", "Age": 1}
{"Name": "Bob", "Age": 1}
The point is that I only get the first line. I am quite new on Go
and I guess the usage of the StdoutPipe is incorrect.