Skip to content

Instantly share code, notes, and snippets.

@nesv
Created December 6, 2012 03:52
Show Gist options
  • Save nesv/4221649 to your computer and use it in GitHub Desktop.
Save nesv/4221649 to your computer and use it in GitHub Desktop.
which(1) implemented in Go
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