Created
November 2, 2020 01:27
-
-
Save jasonmccallister/96388e1835a0d0dd94ba21dfe59ebf67 to your computer and use it in GitHub Desktop.
Changing directories with Go exec.command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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