GOLANG

GO: How to Install Go on Windows 10

Nishee Jaiswal
4 min readJan 18, 2022
Install Go on Windows 10

An environment setup on our local machine is required so that we are able to run, build and compile Go projects.

The operating system which we will be working on is Windows and all the directory structure and folder path descriptions will be based on Windows 10. This environment setup will be similar for other versions of Windows as well.

Let’s start by installing Go on our local machine which is Windows 10.

☛ Step 1: Install Go installer

Open your browser and go to the link https://golang.org/doc/install to get the Golang installer. Click on Download Go for Windows blue card to download the msi file for Windows.

https://golang.org/doc/install

Go to the path of the folder(Generally, it gets downloaded at the Download folder in Windows) where the MSI file gets downloaded and double click on the MSI file.

After opening the MSI file you downloaded follow the prompts to install Go.

By default, the installer will install Go to Program Files or Program Files (x86). You can change the location as needed. After installing, you will need to close and reopen any open command prompts so that changes to the environment made by the installer are reflected at the command prompt.

Now, open your command prompt by clicking on Start Menu Windows icon at the extreme right end corner and search for Command Prompt or cmd. Click on the Command Prompt or press Enter. The Command Prompt window will be opened which is at the C:\Users\<username> location.

To verify that you’ve installed Go, in the Command Prompt window type the following command:

$ go version

Now, confirm that the command prints the installed version of Go as given below.

go version go1.17.2 windows/amd64

☛ Step 2: Create Go Workspace

A workspace is Go’s way to facilitate project management. A workspace, in a nutshell, is a directory on your system where Go looks for source code files, manages dependency packages, and builds distribution binary files.

Go to the following path C:\Users\<your-username> and create a folder inside it by the name go and then go inside this go folder and create three directories by the name pkg, bin and src. This go directory will now be your workspace.

☛ Step 3: Setup Go environment variables

Go to the Start Menu Windows Search icon and type Edit the system environment variable and then click on Open. A window pops up and then click on Environment Variables button.

Environment Variable Window

In the User variables window add a new variable GOPATH and its value will be the path where you have created your go workspace. I have created the go workspace at the following path C:\Users\<your-username>\go

GOPATH                  C:\Users\<your-username>\go

Now, edit the PATH variable and add an entry of %USERPROFILE%\go\bin.

Check in the System variables section inside the PATH variable whether an entry of C:\Users\Program Files\Go\bin entry is present or not. If not then add an entry for the same.

Now, write all your go source code files inside the C:\Users\<your-userprofile>\go\src as whenever a Go program encounters an import statement, it looks for the package in the Go’s standard library ($GOROOT\src). If the package is not available there, then Go refers to the system’s environment variable GOPATH which is the path to the Go workspace directory and looks for packages in $GOPATH\srcdirectory.

You can have as many workspaces as you want, as long as you keep the $GOPATH environment variable pointed to the current working workspace directory whenever you are working on a given project.

☛ Step 4: Write code in Go

Now, you are all set to write code in Go. Open your Visual Studio Code editor on the src folder available on the path C:\Users\<your-userprofile>\go\src .

Now, create your project directory inside the src folder and start writing your first Hello World! program in Go.

Go to the C:\Users\<your-userprofile>\go\srcfolder create a new directory by the name helloworld. Now, open this folder in Visual Studio Code and open a terminal inside it. Make sure to open git bash terminal inside it. Create a file main.go inside the helloworld folder.

Now, write the below code inside your main.go file.

package mainimport "fmt"func main(){
fmt.Println("Hello World!")
}

Execute the below command after saving the main.go file.

MINGW64 ~/go/src/helloworld 
$ go run main.go

Below is the output of the above go source code file on the terminal.

Hello World!

If you are writing your Go code using Visual Studio Code text editor, then install the Go extension inside it.

You have successfully installed Go and you also executed your first Go program on your Windows 10 system. Now, you are on the right path to becoming a Golang developer.

Congratulations! 🎉

--

--