system Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/system/ Mon, 13 Apr 2020 18:44:03 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 system Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/system/ 32 32 159787465 Execute an OS/System Command in Go (Golang) https://vikasboss.github.io/execute-os-system-command-golang/ https://vikasboss.github.io/execute-os-system-command-golang/#respond Mon, 13 Apr 2020 18:43:51 +0000 https://vikasboss.github.io/?p=1991 Overview os/exec package can be used to trigger any OS or system command from Go. It has two functions which can be used to achieve the same Command – Used to create...

The post Execute an OS/System Command in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

os/exec package can be used to trigger any OS or system command from Go. It has two functions which can be used to achieve the same

  • Command – Used to create the cmd object
  • Output – It runs the command and returns the standard output

Code

package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {
    out, err := exec.Command("pwd").Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(out))
}

Output:

It will output the location of current working directory

The post Execute an OS/System Command in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/execute-os-system-command-golang/feed/ 0 1991