Notes
Search…
How To?

How to check if a program is installed or not?

1
import (
2
"os/exec"
3
"testing"
4
)
5
6
func isCommandAvailable(name string) bool {
7
cmd := exec.Command("command", "-v", name)
8
if err := cmd.Run(); err != nil {
9
return false
10
}
11
return true
12
}
13
func TestIsCommandAvailable(t *testing.T) {
14
if isCommandAvailable("ls") == false {
15
t.Error("ls command does not exist!")
16
}
17
if isCommandAvailable("ls111") == true {
18
t.Error("ls111 command should not exist!")
19
}
20
}
Copied!

How to know the path where a program is installed?

1
package main
2
3
import (
4
"fmt"
5
"os/exec"
6
)
7
8
func main() {
9
programName:= "ionice"
10
installationPath,err:=exec.LookPath(programName)
11
12
if err!=nil{
13
fmt.Printf("Failed to detect the path of %q. Reason: %q.\n",programName,err.Error())
14
return
15
}
16
17
fmt.Printf("Program %q installed in %q directory.\n",programName,installationPath)
18
}
Copied!

How to force go build to use vendor?

Use -mod vendor parameter while building.
1
go build -mod vendor ./...
Copied!