Sling Academy
Home/Golang/How to set up and run Go in Windows

How to set up and run Go in Windows

Last updated: November 20, 2024

Go, commonly referred to as Golang, is a programming language developed by Google. It's known for its simplicity and efficiency. Setting up Go on a Windows machine is straightforward and we'll guide you through the steps from basic to advanced examples.

Basic Setup

Step 1: Download Go

First, you need to download the Go installer for Windows. Visit the official Go download page and download the Windows installer.

Step 2: Install Go

Run the downloaded installer and follow the on-screen instructions. By default, Go is installed in C:\Go. Make sure to check the option to add Go to your PATH

Step 3: Verify Your Installation

After installation, open a command prompt and type:

go version

This should display the installed version of Go, indicating that Go has been correctly installed.

Intermediate Configuration

Step 4: Setting up the Workspace

It's essential to set up a Go workspace where you will manage your projects efficiently:

mkdir %USERPROFILE%\go

The environment variable GOPATH should be set to this directory. You can set it permanently by adding the following to your system environment variables:

set GOPATH=%USERPROFILE%\go

Step 5: Update Environment Variables

Ensure the bin directory of GOPATH is in your PATH. Add %USERPROFILE%\go\bin to your system's PATH variable from System Properties.

Advanced: Running and Testing Your First Go Program

Step 6: Create a Simple Go Program

Create a new directory within your workspace to store the Go program:

mkdir %USERPROFILE%\go\src\helloworld

Create a file named hello.go in that directory with the following content:

package main

import "fmt"

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

Step 7: Run Your Go Program

In the command prompt, navigate to the location of hello.go and execute:

cd %USERPROFILE%\go\src\helloworld
go run hello.go

This should output Hello, World! on the prompt.

Step 8: Compile Your Go Program

To compile your program to an executable, run:

go build hello.go

This will produce an executable file (hello.exe) in the same directory which can be run:

hello.exe

Running this executable will also output Hello, World!.

You've successfully set up, written, and run a Go program on Windows!

Next Article: How to set up and run Go in MacOS

Previous Article: What is Go (Golang) and why you should learn it?

Series: Getting Started with Golang

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant