[Golang] Run wget via Shell Command


In your Go program, you can download things via hget or pget, which is pure Go implementation and portable in all platforms. But if you do not care about portability and just want to use wget on your Linux or Unix-like platform, we can use Go standard os/exec package to run wget via shell command. The following is howto:

import (
      "os"
      "os/exec"
)

// Call shell command wget to download. The reason to use wget is that wget
// supports automatically resume download. So this package only runs on Linux
// systems.
func wget(url, filepath string) error {
      // run shell `wget URL -O filepath`
      cmd := exec.Command("wget", url, "-O", filepath)
      cmd.Stdout = os.Stdout
      cmd.Stderr = os.Stderr
      return cmd.Run()
}

Tested on: Ubuntu Linux 17.10, Go 1.10.


References:

[1]
[2]
[3]Help! os/exec Output() on Non-English Windows cmd! : golang