Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jasonmccallister
Created November 2, 2020 01:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonmccallister/96388e1835a0d0dd94ba21dfe59ebf67 to your computer and use it in GitHub Desktop.
Save jasonmccallister/96388e1835a0d0dd94ba21dfe59ebf67 to your computer and use it in GitHub Desktop.
Changing directories with Go exec.command
package main
import (
"flag"
"log"
"os"
"os/exec"
)
func main() {
dir := flag.String("dir", "", "the directory to list contents")
flag.Parse()
cmd := exec.Command("ls", "-la")
if *dir != "" {
cmd.Dir = *dir
}
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment