Sure, capturing console output in Go is quite simple. You can do this using the os/exec
package.
Here’s an example code snippet that demonstrates how to capture console output in Go:
package main
import (
"bytes"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("ls", "-l")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(out.String())
}
In this example, we are running the ls -l
command and capturing its output in a buffer. The captured output is then printed to the console using the fmt.Println()
function.
You can replace "ls"
with any other command you want to run and capture its output.
I hope this helps! Let me know if you have any further questions.