Installing Go on Windows 11 and Running Your First "Hello World" Program

Installing Go on Windows 11 and Running Your First "Hello World" Program

ยท

3 min read

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

  1. Visit the Official Go Website:

  2. Download the Installer:

    • Click on the Windows download link to get the installer (e.g., go1.22.5.windows-amd64.msi).
  3. 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

  1. Open Command Prompt:

    • Press Win + R, type cmd, and hit Enter.
  2. Check the Go Version:

    • Type go version and press Enter.

    • You should see the installed Go version (e.g., go version go1.22.5 windows/amd64).

Step 3: Setting Up Your Go Workspace

  1. 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
      
  2. 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, select Properties, then Advanced system settings, and finally Environment 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

  1. 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
      
  2. 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

  1. Navigate to Your Program's Directory:

    • Open Command Prompt and navigate to the directory containing hello.go:

        cd C:\Users\YourUsername\GoProjects\hello
      
  2. 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, the main 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!

Did you find this article valuable?

Support NavyaDevops by becoming a sponsor. Any amount is appreciated!

ย