How to use os exec package

Use the os/exec package to spawn external processes by calling exec.Command and running them with .Output() or .Run().

The os/exec package is not a configuration tool; it is a standard library for spawning and running external processes like shell commands. To use it, import "os/exec" and call exec.Command with the program name and its arguments, then run the command using .Run() or .Output().

package main

import (
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("ls", "-l")
	output, err := cmd.Output()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(output))
}