Skip to content

Instantly share code, notes, and snippets.

@leonmwandiringa
Forked from miguelmota/command_exists.go
Created November 30, 2019 17:12
Show Gist options
  • Save leonmwandiringa/9d17fc8a270a53ea17a6e989a03f90a3 to your computer and use it in GitHub Desktop.
Save leonmwandiringa/9d17fc8a270a53ea17a6e989a03f90a3 to your computer and use it in GitHub Desktop.
Golang check if command exists
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