How to Install Golang on Windows, Linux & Mac
Lets install golang on your machine
The first thing that needs to be done before you can use Go is to install it first. The actual installation guide has been provided on the Golang official website http://golang.org/doc/install#install . Here the author tries to summarize the installation instructions on the link, so it is easier to follow, especially for readers who are just learning.
Go used is version 1.12.7 . It is recommended to use this version, or other versions of at least 1.11 and above.
Link to download the installer go: https://golang.org/dl/ . You can directly download it from the URL and then do the installation yourself, or you can follow the instructions in this chapter. First download the installer at https://golang.org/dl/ . Select the installer for the Windows OS according to the type of bit used. After downloading, run the installer, click next until the installation process is complete. By default if you don't change the path during installation, Golang will be installed installed on C:\go. The path is automatically registered in the variable path .
Add the bin subdirectory of your Go root (for example, c:\Go\bin) to your PATH environment variable.
Open Command Prompt / CMD , execute the command to check the Go version.
If the output is the same as Go installed, it indicates that the installation was successful.
It often happens that the command go version cannot be run even if the installation is successful. The solution can be to restart CMD (close CMD, then reopen). After that, try to run the command again.
Check that Go is installed correctly by setting up a workspace and building a simple program, as follows.
Next, make the directory src\hello inside your workspace, and in that directory create a file named hello.go that looks like:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
Then build it with the go tool:
C:\> cd %USERPROFILE%\go\src\hello
C:\Users\Gopher\go\src\hello> go build
The command above will build an executable named hello.exe in the directory alongside your source code. Execute it to see the greeting:
C:\Users\Gopher\go\src\hello> hello
hello, world
If you see the "hello, world" message then your Go installation is working.
You can run go install to install the binary into your workspace's bin directory or go clean -i to remove it.
The easiest way to install Go on Mac OS is to use homebrew . Install Homebrew first (if it doesn't already exist), run the command in the terminal
Install Go using the command brew.
Add the path to the environment variable.
$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bash_profile
$ source ~/.bash_profile
Run the command to check the Go version.
If the output is the same as Go installed, it indicates that the installation was successful.
Download the installer archive from the https://golang.org/dl/ link , select the linux installer that matches your computer's bit type. Downloading can be done via CLI, using wget or curl. Open the terminal , extract the archive to /usr/local.
$ tar -C /usr/local -xzf go1...
After that export path, use the command below.
$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
$ source ~/.bashrc
Next, execute the following command to test whether Go has been installed correctly.
If the output is the same as Go installed, it indicates that the installation was successful.