Introduction
In this blog, we'll guide you through the process of installing Golang on Windows 11 and writing your first Go program, "Hello World." Follow these steps to get started with Golang on your Windows machine.
Step 1: Downloading and Installing Go
Visit the Official Go Website:
- Go to the official Go download page.
Download the Installer:
- Click on the Windows download link to get the installer (e.g.,
go1.22.5.windows
-amd64.msi
).
- Click on the Windows download link to get the installer (e.g.,
Run the Installer:
Double-click the downloaded
.msi
file to run the installer.Follow the prompts to complete the installation. The installer will automatically add Go to your system PATH.
Step 2: Verifying the Installation
Open Command Prompt:
- Press
Win + R
, typecmd
, and hitEnter
.
- Press
Check the Go Version:
Type
go version
and pressEnter
.You should see the installed Go version (e.g.,
go version go1.22.5 windows/amd64
).
Step 3: Setting Up Your Go Workspace
Create a Workspace Directory:
Open Command Prompt and navigate to your preferred location.
Create a new directory for your Go projects:
mkdir GoProjects cd GoProjects
Set the GOPATH Environment Variable:
The GOPATH is the root directory of your workspace.
You can set it in your environment variables. Right-click on
This PC
, selectProperties
, thenAdvanced system settings
, and finallyEnvironment Variables
.Add a new user variable named
GOPATH
and set its value to the path of your workspace directory (e.g.,C:\Users\YourUsername\GoProjects
).
Step 4: Writing Your First Go Program
Create a New Directory for Your Program:
Inside your workspace directory (
GoProjects
), create a new directory for your "Hello World" program:mkdir hello cd hello
Create the Go Source File:
Open your favorite text editor or IDE (such as Visual Studio Code).
Create a new file named
hello.go
and add the following code:package main import "fmt" func main() { fmt.Println("Hello, World!") }
Step 5: Running Your Go Program
Navigate to Your Program's Directory:
Open Command Prompt and navigate to the directory containing
hello.go
:cd C:\Users\YourUsername\GoProjects\hello
Build and Run the Program:
To compile and run your program, use the following command:
go run hello.go
You should see the output:
Hello, World!
Explanation of the "Hello World" Program
package main: Defines the package name. The
main
package is a special package in Go that defines a standalone executable program.import "fmt": Imports the
fmt
package, which provides formatting functions for input and output.func main(): The
main
function is the entry point of the program. When you run the program, themain
function is executed.fmt.Println("Hello, World!"): Prints the string "Hello, World!" to the console. The
Println
function adds a newline character at the end.
Conclusion
Congratulations! You've successfully installed Go on Windows 11 and run your first Go program. In this blog, we've covered the installation steps, setting up your workspace, and writing and running a simple "Hello World" program.
Stay tuned for more tutorials and tips on mastering Golang! Happy coding!