Skip to content

Commit b1b26d6

Browse files
committed
9-filesystem-basic/which
1 parent 9cffd67 commit b1b26d6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

9-filesystem-basic/which/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/kyu08/go-system-programming/which
2+
3+
go 1.22.3

9-filesystem-basic/which/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
func main() {
10+
if len(os.Args) != 2 {
11+
fmt.Printf("%s [exec path]\n", os.Args[0])
12+
os.Exit(1)
13+
}
14+
15+
for _, path := range filepath.SplitList(os.Getenv("PATH")) {
16+
execpath := filepath.Join(path, os.Args[1])
17+
_, err := os.Stat(execpath)
18+
if !os.IsNotExist(err) {
19+
fmt.Println(execpath)
20+
return
21+
}
22+
}
23+
os.Exit(1)
24+
}

0 commit comments

Comments
 (0)