Installing go lang
On Windows
- Download the latest version of go MSI installer from https://golang.org/dl/ best suited for your system architecture.
- Start the installation by double-clicking on the installer and installer wizard will open as given in the following figure.
- For the installation follow the wizard instructions.
- Note that when you installing go lang using installer then it will ask the location where go will be installed as shown in the figure below.
- Installation directory of go lang is called GOROOT here the GOROOT is c:\go directory.
- After this follow the wizard instructions and complete the go lang installation.
Test Go installation
- To Check that Go is installed correctly or not, you need to create a go workspace (a place /folder/directory in which you save you go programs) and create a sample go program and run the program using the command line. The sample program is given below...
- You can create a sample program by using any simple text editor like notepad, notepad++, sublime or anyone you like and save the program in a workspace with the extension .go.
- We suppose that the sample program is saved with hello.go
package main
import "fmt"
func main() {
fmt.Print("Hello go lang")
}
Running go program
- To run the go program you need to set up the GOPATH environment variable with your workspace path.
- Default go path is $HOME/go, Where $HOME is the environment variable or path which refers to users home directory, For example: if my window current username is Mahesh then $HOME refers to C:\Users\Mahesh by default. So the default GOPATH will be C:\Users\Mahesh\go.
- If you want to change the GOPATH then Visit (Right click on This PC (My Computer) > Properties > Advance system setting > Environment Variables). This will open a popup as shown in the figure below. This popup has two sections, the top section is for current users which are for logged in users and the bottom section is for all users of the system. you can create your environment variable in any section based on the permission you want to allow.
- To create a new environment variable click on the New button, then Name the environment variable is GOPATH and value is Your workspace path and then click ok to close the popup and save the settings.
- If GOPATH is already existed you can edit the GOPATH environment variable.
- After setting the environment variable, open the command prompt (User Window + R > Type CMD).
- Then switch your location with your workspace.
- Then compile the program by using go build hello.go (hello.go is the program name. Successful compilation with generate hello.exe in the same directory.
- To run the go program, run hello.exe by typing hello or hello.exe on cmd.
- Alternatively, you can run go program directly with go run hello.go. This will automatically compile and run the go program.
Comments