Notes
  • Overview
  • Go
    • Basics
    • How To?
    • Tools & Libraries
    • Tutorials
    • Go Modules
    • Design Patterns
    • Guidelines
      • Code Review and Comments
    • Conversion
      • map[string]interface{} to JSON
      • map[string]interface{} to struct
    • Tips & Tricks
  • Docker
    • Blog Posts
    • Commands
  • Kubernetes
    • Courses
    • Concepts
    • Networking
      • Ingress Controller
    • KIND
    • Custom Resources
    • Tips & Tricks
      • Commands
        • Log
        • List
      • Container
        • Inject Executable Script
      • RBAC
        • Cross namespaced access
      • Patching Object
        • Restrict Key
      • Copy Object
  • Git
    • Rebase
  • System Design
    • Pub-Sub
      • Ovserver vs PubSub
  • Distributed System
    • Untitled
  • Linux
    • Bash
      • Basics
      • How To?
      • Pipe
      • Tips & Tricks
  • Security
    • Untitled
  • Database
    • PostgreSQL
      • Tricks & Tips
    • MySQL
      • Tips & Tricks
  • MISC
    • Development Patterns
    • Useful Github Apps/Actions
    • Parameters vs Arguments
    • Open API
  • Terminal
  • VS Code
Powered by GitBook
On this page
  • How to check if a program is installed or not?
  • How to know the path where a program is installed?
  • How to force go build to use vendor?

Was this helpful?

  1. Go

How To?

How to check if a program is installed or not?

import (
      "os/exec"
      "testing"
)

func isCommandAvailable(name string) bool {
      cmd := exec.Command("command", "-v", name)
      if err := cmd.Run(); err != nil {
              return false
      }
      return true
}
func TestIsCommandAvailable(t *testing.T) {
      if isCommandAvailable("ls") == false {
              t.Error("ls command does not exist!")
      }
      if isCommandAvailable("ls111") == true {
              t.Error("ls111 command should not exist!")
      }
}

How to know the path where a program is installed?

package main

import (
  "fmt"
  "os/exec"
)

func main() {
  programName:= "ionice"
  installationPath,err:=exec.LookPath(programName)
  
  if err!=nil{
    fmt.Printf("Failed to detect the path of %q. Reason: %q.\n",programName,err.Error())
    return
  }

  fmt.Printf("Program %q installed in %q directory.\n",programName,installationPath)
}

How to force go build to use vendor?

Use -mod vendor parameter while building.

go build -mod vendor ./...
PreviousBasicsNextTools & Libraries

Last updated 4 years ago

Was this helpful?