Problem when outputting from stdout line by line

When I try to read from stdout with normal output all works fine, but when I read from a line the output hangs and exit with Exit code 2, I use StdoutPipe and I read that pipe has a default limit of 65536 byte, so maybe this is the reason for the interruption? the data flies very fast and when reading polyline probably just does not have time to read and pipe overflows? I also tried to copy stdout to the buffer, but it still happens the same program hangs and then kills in general what to do in this case, do you know?

cmd := exec.Command("tshark", "-i", d.device, "-T", "json")
stdoutReader, err := cmd.StdoutPipe()
if err != nil {
  log.Println(err)
}
go func() {
  var b bytes.Buffer
  if _, err := io.Copy(&b, stdoutReader); err != nil {
    log.Println(err)
 }
}()
scanner := bufio.NewScanner(stdoutReader)
done := make(chan bool)
go func() {
  for scanner.Scan() {
    fmt.Println(scanner.Bytes())
  }
  done <- true
}()
if err := cmd.Start(); err != nil {
  log.Println(err)
}
<-done
if err = cmd.Wait(); err != nil {
  log.Println(err)
}

solved the problem with bufio.NewReader(stdout), no buffer is needed for copying, everything works and there is no interruption due to pipe overflow

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.