-
-
Save d1y/7646bacd4080c83265e39f78d578dc26 to your computer and use it in GitHub Desktop.
Golang check if command exists
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 ( | |
"log" | |
"os/exec" | |
) | |
func main() { | |
path, err := exec.LookPath("ls") | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println(path) // bin/ls | |
} | |
// as util | |
func commandExists(cmd string) bool { | |
_, err := exec.LookPath(cmd) | |
return err == nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment