🌻Getting Started with Go: Setting Up Your Go Language Environment

🌻Getting Started with Go: Setting Up Your Go Language Environment

#90DaysOfDevOps

Day-8: Learning a Programming Language - GO

Go, also known as Golang, is an authoritative and efficient programming language developed by Google. It offers simplicity, readability, and excellent performance, making it an ideal elite for building scalable and reliable applications. In this blog post, here's the process of setting up a Go language environment on your PC, so you can start exploring the world of Go and unleashing its potential.

Download and Install Go:

Go to the official Go website https://go.dev/dl/

Choose the appropriate package for your operating system (Windows, macOS, or Linux) and architecture (32-bit or 64-bit). Once downloaded, run the installer and follow the instructions to complete the installation.

Set Up Go Environment Variables:

To use Go from any command prompt or terminal window, you need to set up the Go environment variables. These variables include the Go binary path and the workspace directory.

For Windows:

  1. Open the Control Panel and go to "System and Security" > "System" > "Advanced system settings".

  2. Click on the "Environment Variables" button.

  3. Under "System variables", click "New" and add a new variable called "GOROOT" with the value set to the Go binary path (e.g., C:\Go).

  4. Click "New" again and add another variable called "GOPATH" with the value set to the directory where you want to keep your Go workspace (e.g., C:\Users\YourUsername\go).

  5. Edit the "Path" variable and append "%GOROOT%\bin;%GOPATH%\bin" at the end.

  6. Click "OK" to save the changes.

For macOS and Linux:

  1. Open a terminal window.

  2. Edit the ~/.bash_profile or ~/.bashrc file using your preferred text editor (e.g., nano ~/.bash_profile).

  3. Add the following lines at the end of the file:

  4.     export GOROOT=/usr/local/go
        export GOPATH=$HOME/go
        export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    
  5. Save the file and exit the text editor.

  6. Run the command "source ~/.bash_profile" or "source ~/.bashrc" to apply the changes.

Verify Your Installation:

To ensure that Go is installed correctly and the environment variables are set up properly, open a new command prompt or terminal window and run the following command:

go version

If everything is set up correctly, you should see the installed Go version displayed on the screen.

Create Your First Go Program:

Now that your Go environment is ready,

let's create a simple "Hello, World!" program to test it out. Open a text editor and create a new file called "hello.go" with the following content:

package main

import "fmt"

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

Save the file and navigate to the directory where you saved it using the command prompt or terminal. Then, execute the following command:

go run hello.go

If everything went smoothly, you should see "Hello, World!" printed on the screen/terminal.

Did you find this article valuable?

Support Jatin Chourasia by becoming a sponsor. Any amount is appreciated!