I am looking for a function to find a binary in all folders which are available in $PATH.
I know i can use os.Getenv("PATH") but it returns:
path: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/go/bin
Which needs to be processed.
Ideally function like this: FindBinary("ntpq") and it will return the path + binary name and false when not found in path.
Does anybody have a ready function?
You can use the LookPath
function from the os/exec
package:
path, err := exec.LookPath("fortune")
if err != nil {
log.Fatal("installing fortune is in your future")
}
fmt.Printf("fortune is available at %s
", path)