package main
import (
"bufio"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("ls", "-l")
// Create a pipe to read the command's output
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
// Start the command
err = cmd.Start()
if err != nil {
panic(err)
}
// Create a channel to receive the lines of output
outputChan := make(chan string)
// Start a goroutine to read the command's output and send it to the channel
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
outputChan <- scanner.Text()
}
if err := scanner.Err(); err != nil {
panic(err)
}
// Close the channel to signal to the main goroutine that there is no more data to be received
close(outputChan)
}()
// Process the lines of output as they are received on the channel
for line := range outputChan {
fmt.Println(line)
}
// Wait for the command to finish
err = cmd.Wait()
if err != nil {
panic(err)
}
}