Created
December 6, 2012 03:52
-
-
Save nesv/4221649 to your computer and use it in GitHub Desktop.
which(1) implemented in Go
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" | |
"os" | |
"path" | |
"strings" | |
) | |
func main() { | |
flag.Parse() | |
var progToFind string | |
if len(flag.Args()) >= 1 { | |
progToFind = flag.Args()[0] | |
} | |
if envpaths := os.Getenv("PATH"); len(envpaths) > 0 { | |
dirs := strings.Split(envpaths, ":") | |
for _, dir := range dirs { | |
testPath := path.Join(dir, progToFind) | |
_, err := os.Stat(testPath) | |
if err == nil { | |
println(testPath) | |
return | |
} | |
if os.IsNotExist(err) { | |
continue | |
} | |
} | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment