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 versionThis 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%\goThe 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%\goStep 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\helloworldCreate 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.goThis should output Hello, World! on the prompt.
Step 8: Compile Your Go Program
To compile your program to an executable, run:
go build hello.goThis will produce an executable file (hello.exe) in the same directory which can be run:
hello.exeRunning this executable will also output Hello, World!.
You've successfully set up, written, and run a Go program on Windows!