iTranslated by AI
How to Install Go on Windows
Introduction
In this article, I will explain how to set up a Go development environment on Windows.
Installing Go itself is easy, but you need to be careful about the installation location and settings.
By default, the installation directory is C:\Program Files\Go and GOPATH is set to C:\Users\<username>\.go.
In this case, since the directory contains spaces, some tools might not recognize the path correctly.
Additionally, GOPATH stores configuration and package files directly under the home directory; however, since several directories already exist there, managing them becomes cluttered.
Because these default settings can be inconvenient, we will install Go to C:\lang\go this time.
GOPATH will also be set to C:/Users/<username>/.local/share/go in accordance with the XDG Base Directory specification.
1. Environment Preparation
1.1. Go Installation Path
I generally install programming languages under C:\lang\. Since we are installing Go, the path will be C:\lang\go.
C:\lang, unlike the typical Windows installation path C:\Program Files, does not contain spaces in the directory name.
Since directory names containing spaces can cause unexpected behavior in some tools, it is recommended to avoid them. You can prevent issues by using a directory name without spaces.
1.2. XDG Base Directory
The XDG Base Directory specification is a standard for defining where user-specific data and configuration files should be stored. It is supported by default in UNIX/Linux environments, and many applications store their configuration files according to XDG.
The XDG standard can also be utilized in Windows environments.
We set environment variables such as XDG_CONFIG_HOME, XDG_DATA_HOME, XDG_CACHE_HOME, and XDG_STATE_HOME.
By preparing the directory structure accordingly, various applications can store their configuration files under the XDG hierarchy.
2. Installing Go
2.1. Installing Go via winget
Go is compatible with winget, the package manager for Windows.
By using winget, you don't need to download and run an installer via a browser; you can install it quickly by running winget in the command line.
winget has a --location option to specify the installation path, but the Go installer ignores it.
Therefore, we use a variable called INSTALLDIR.
Install Go using the following command:
winget install GoLang.Go --override "INSTALLDIR=C:\\lang\\go"
2.2. Setting Environment Variables
To use Go, you need to set certain environment variables. The environment variables and their values are as follows:
| Environment Variable | Value | Description |
|---|---|---|
| GOPATH | C:/Users/<username>/.local/share/go |
Path to the Go workspace. Set under XDG_DATA_HOME. |
| PATH | ...;C:\lang\go\bin;%GOPATH%\bin |
Add C:\lang\go\bin and %GOPATH%\bin to Path
|
Note
- Do not reference environment variables when setting GOPATH. If the path becomes a value like "%XDG_...%" instead of the actual directory, the created Go binaries will not be found.
- You need to restart to apply the settings to Windows.
How to set environment variables
You can set environment variables from the [System Properties] dialog.
The following steps describe how to set environment variables on Windows:
-
Right-click the [Start menu] and select [System].
-
Click [Advanced system settings].
-
The "System Properties" dialog appears; click [Environment Variables].
-
The "Environment Variables" dialog appears; select the environment variable you want to edit and click [Edit].
-
Edit the necessary environment variables and click [OK].
-
Click [OK] on each dialog to close them.
This completes the environment variable setup.
For specific settings, refer to the table in Setting Environment Variables.
2.3. Verifying Go Operation
If the environment variables are set correctly, Go will work properly.
The go version command outputs the current version of Go.
Run go version to confirm that Go is working correctly.
> go version
go version goX.XX.X windows/amd64
>
As shown above, the Go version is displayed.
3. Your First Go Program
3.1. "Hello, World!!" with Go
Follow these steps to create a program that outputs "Hello, World!!" in Go:
-
Create a Go module directory
mkdir hello & cd helloCreate a new
hellodirectory for your program and move into it. -
Initialize the Go module
go mod init helloThe
go mod initcommand creates and initializes a new Go module.
In this case, we initialize thehellodirectory as the root of the Go module and create ago.modfile. -
Create a
hello.gofile and write the code as follows:hello.gopackage main import "fmt" func main() { fmt.Println("Hello, World!!"); }Create a Go language program within the Go module.
-
Run the Go program using the
go runcommandgo run hello.goUse the
go runcommand to execute the program you created. -
The message "Hello, World!!" is displayed
If the message is displayed as shown above, you have successfully created a program in Go.
Conclusion
So far, we have installed Go and created a simple program.
Now you can start programming with Go.
Go is a simple and powerful language that can be used to develop various applications.
I hope you use this article as a reference to set up your own development environment and explore the world of Go.
Happy Hacking!
Technical Terms and Annotations
- GOPATH: The Go language workspace directory. This is the location where Go projects are stored.
- XDG Base Directory: A unified standard for defining storage locations for configuration files and other data. This helps organize where configuration files are saved.
- winget: A package manager for Windows. It allows you to install applications from the Windows command line.
References
Websites
- Official Go Website: https://go.dev/
- XDG Base Directory: https://wiki.archlinux.jp/index.php/XDG_Base_Directory
- winget Package Manager: https://github.com/microsoft/winget-cli
Discussion