こんにちは、kurumi-bioです。
第1回目のexecパッケージ(標準ライブラリー)の学習です。
環境
- Windows
OSバージョン:Windows11 Home 22H2
Go言語のバージョン:go version go1.20 windows/amd64 - Linux
OSバージョン:openSUSE Leap 15.4
Go言語のバージョン:go version go1.20.1 linux/amd64
Variables(変数)
var ErrDot = errors.New("cannot run executable found relative to current directory")
var ErrNotFound = errors.New("executable file not found in $PATH")
var ErrWaitDelay = errors.New("exec: WaitDelay expired before I/O complete")
LookPath関数
func LookPath(file string) (string, error)
◆テストコード
package main import ( "fmt" "os" "os/exec" ) func main() { if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, "usage: LookPathTest.exe fileName") os.Exit(1) } s, e := exec.LookPath(os.Args[1]) if e != nil { fmt.Fprintf(os.Stderr, "ERROR=[%v]\n", os.NewSyscallError("os.LookPath", e)) os.Exit(1) } else { fmt.Printf("path=[%s]\n", s) } }
◆実行結果(Windows)
◆テストコード
package main import ( "errors" "fmt" "os" "os/exec" ) func main() { if len(os.Args) != 2 { fmt.Fprintln(os.Stderr, "usage: LookPathTest.exe fileName") os.Exit(1) } s, e := exec.LookPath(os.Args[1]) if s != "" { fmt.Printf("path=[%s]\n", s) } if e != nil { if errors.Is(e, exec.ErrDot) { fmt.Fprintln(os.Stderr, "ErrorDot") } else if errors.Is(e, exec.ErrNotFound) { fmt.Fprintln(os.Stderr, "ErrNotFound") } else if errors.Is(e, exec.ErrWaitDelay) { fmt.Fprintln(os.Stderr, "ErrWaitDelay") } fmt.Fprintf(os.Stderr, "ERROR=[%v]\n", os.NewSyscallError("os.LookPath", e)) } }
◆実行結果(Windows)
◆実行結果(Linux)
最後までご覧いただきありがとうございます