Learn Go · 1/18

Introduction to Go & Environment Setup

9 min read

Introduction to Go & Environment Setup Course: Go

1. The 'Why' Before the 'How': Why Go? 🤔

Ever wondered what makes Go, also known as Golang, a popular choice for developers? Let's dive into the reasons why this language, born at Google, has been gaining so much traction.

The Problem: In the world of software development, engineers often face a trade-off between a language's performance and its ease of use. Languages like C++ offer fantastic performance but can be complex to write and manage. On the other hand, languages like Python are praised for their simplicity and readability but might not always deliver the raw speed needed for certain applications.

Go's Solution: Go was designed to bridge this gap. It offers the speed of a compiled language like C++ with the readability and developer-friendliness of a language like Python. This makes it an excellent choice for building high-performance systems without sacrificing developer productivity.

Key Features that Make Go Shine:

  • Concurrency: Imagine a coffee shop with a single barista trying to take orders, make coffee, and handle payments all at once. It would be slow and inefficient. Go's built-in concurrency features, called "goroutines" and "channels," are like having multiple baristas working simultaneously and communicating effectively. This makes it incredibly efficient for applications that need to handle many tasks at once, like web servers and data pipelines.
  • Simplicity and Readability: Go's syntax is intentionally clean and minimal. This makes the code easier to read, understand, and maintain, which is a huge advantage, especially in large teams.
  • Fast Compilation: Go compiles directly to machine code, which means your programs start up and run incredibly fast. This is a welcome change for developers coming from languages with slower build times.
  • Garbage Collection: Go automatically manages memory for you, which helps prevent common bugs and makes development easier. You can focus on writing your application's logic without getting bogged down in manual memory management.
  • A Rich Standard Library: Go comes with a comprehensive standard library, providing a wide range of built-in functionalities for everything from networking to file I/O. This means you can often build powerful applications without relying heavily on third-party libraries.

An Interesting Fact: Go was created at Google by some of the same engineers who developed the C programming language, Unix, and UTF-8. They wanted to create a language that addressed the challenges of modern software development at Google's scale.

2. Setting up the Go Workspace and Modules 🛠️

Before you can start writing Go code, you need to set up your development environment. This involves installing Go and understanding how to structure your projects.

The Problem: In any programming language, you need a way to organize your code and manage dependencies (the libraries and packages your code relies on). Without a standardized system, projects can become messy and difficult to maintain.

Go's Solution: Modules: Go uses a system called "modules" to manage dependencies. A module is a collection of Go packages that are released together. A go.mod file at the root of your project declares the module's path and its dependencies.

Step-by-Step Installation Guide:

  1. Download Go: The first step is to download the official Go distribution for your operating system (Windows, macOS, or Linux) from the official Go website.

  2. Install Go:

    • Windows: Run the MSI installer and follow the prompts. It will typically install Go in C:\Go. The installer will also automatically add the Go binary to your system's PATH environment variable.
    • macOS: Use the package installer, which will install Go to /usr/local/go and add /usr/local/go/bin to your PATH.
    • Linux: Extract the downloaded archive into /usr/local, and then add /usr/local/go/bin to your PATH environment variable.
  3. Verify the Installation: Open a new terminal or command prompt and type the following command:

    bash
    go version

    If the installation was successful, you will see the installed Go version printed to the console.

Creating Your First Go Module:

  1. Create a Project Directory: Outside of the $GOPATH/src directory, create a new folder for your project.

    bash
    mkdir hello-world
    cd hello-world
  2. Initialize the Module: Inside your project directory, run the go mod init command, followed by the name of your module. This is typically your project's repository URL, but for now, you can use a simple name.

    bash
    go mod init example.com/hello-world

    This command creates a go.mod file in your directory. This file will track your project's dependencies.

Now you have a properly configured Go workspace, and you're ready to write your first program!

3. Your First Go Program: 'Hello World' 👋

It's a time-honored tradition in programming to start with a "Hello, World!" program. This simple program will help you understand the basic structure of a Go file.

The Problem: You have your Go environment set up, but how do you actually write and run a Go program?

The Solution: A Simple "Hello, World!" Program:

  1. Create a Go File: Inside your hello-world directory, create a new file named main.go. Go files always have a .go extension.

  2. Write the Code: Open main.go in your favorite text editor and add the following code:

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

Breaking Down the Code:

  • package main: Every Go program starts with a package declaration. The main package is special; it tells the Go compiler that this is an executable program (a program you can run).
  • import "fmt": The import keyword is used to include packages that contain pre-written code. The fmt package (short for "format") provides functions for formatting and printing text to the console.
  • func main(): The main function is the entry point of your program. When you run your program, the code inside the main function is what gets executed.
  • fmt.Println("Hello, World!"): This line calls the Println function from the fmt package to print the string "Hello, World!" to the console, followed by a new line.

Running Your Program:

Open your terminal, make sure you are in the hello-world directory, and use the go run command:

bash
go run main.go 

You should see the following output:

autohotkey
Hello, World! 

Congratulations! You've successfully written and executed your first Go program.

4. Understanding the CLI: go run, go build, and go mod 🏃‍♂️

The Go command-line interface (CLI) is a powerful tool for managing your Go projects. Let's look at three of the most frequently used commands.

The Problem: You know how to run a simple Go program, but how do you compile it into a distributable file? And how do you manage the dependencies your project might need?

The Solution: The Go CLI:

  • go run: As you've already seen, go run compiles and runs your Go program in one step. This is great for quick testing and development. Under the hood, it compiles your code into a temporary executable file and then runs it.

  • go build: The go build command compiles your Go code into an executable file.

    bash
    go build

    This command will create an executable file in your current directory. On Windows, it will be named hello-world.exe, and on macOS and Linux, it will simply be hello-world. You can then run this file directly from your terminal.

    The advantage of go build is that it creates a standalone executable that you can share and run on other machines with the same operating system and architecture, even if they don't have Go installed.

  • go mod: The go mod command is your primary tool for managing dependencies.

    • go mod init: Initializes a new module.
    • go get: Adds a new dependency or updates an existing one. For example, to add the popular rsc.io/quote package, you would run:
      bash
      go get rsc.io/quote
      This command will download the package and add it as a dependency in your go.mod file.
    • go mod tidy: This is a very useful command that cleans up your go.mod file. It adds any missing dependencies and removes any unused ones.

Real-World Scenario:

Imagine you're building a web server. You'll likely use external packages to handle things like routing and handling HTTP requests. You would use go get to add these packages to your project. When you're ready to deploy your server, you'd use go build to create a single executable file that you can easily run on your server. During development, you'd frequently use go run to test your changes quickly.

By mastering these fundamental CLI commands, you'll have a solid foundation for building and managing your Go projects effectively.

References

Why Go? 5 Reasons for Programmers to Consider Go/Golang - Nyla Technology Solutions. (n.d.). Retrieved February 25, 2026, from https://www.nylasolutions.com/company/blog/5-reasons-programmers-should-consider-go-golang
Tutorial: Get started with Go - The Go Programming Language. (n.d.). Retrieved February 25, 2026, from https://go.dev/doc/tutorial/getting-started
Getting Started with Go: Write Your First Hello World Program in Go - ByteSizeGo. (2024, November 4). Retrieved February 25, 2026, from https://bytesizego.com/getting-started-with-go-hello-world
What Is Go Programming Language and What Is It Used For? - Coursera. (2026, February 21). Retrieved February 25, 2026, from https://www.coursera.org/articles/go-programming-language
Why Go is Popular Right Now and Why I Started Learning Go as a Node.js Developer. (2024, October 22). Retrieved February 25, 2026, from https://medium.com/@thisisbang/why-go-is-popular-right-now-and-why-i-started-learning-go-as-a-node-js-developer-de24b1a457c6
15 Reasons I Love Go - Applied Go. (n.d.). Retrieved February 25, 2026, from https://appliedgo.com/blog/15-reasons-i-love-go
Go Hello World: Your First Practice Guide to Learn Golang - igmGuru. (2026, February 24). Retrieved February 25, 2026, from https://www.igmguru.com/blog/go-hello-world/
Go (programming language) - Wikipedia. (n.d.). Retrieved February 25, 2026, from https://en.wikipedia.org/wiki/Go_(programming_language)
Getting Started with Go and the Web: Hello, World! - DEV Community. (2024, December 28). Retrieved February 25, 2026, from https://dev.to/adiatiayu/getting-started-with-go-and-the-web-hello-world-5dla
The Essential Go Command Guide for Developers. (2025, July 25). Retrieved February 25, 2026, from https://www.cockroachlabs.com/blog/go-command-guide/
Hello World in Golang - GeeksforGeeks. (2025, July 12). Retrieved February 25, 2026, from https://www.geeksforgeeks.org/hello-world-in-golang/
Go Modules: A Beginner's Guide in 2026 - igmGuru. (2025, December 11). Retrieved February 25, 2026, from https://www.igmguru.com/blog/go-modules/
Creating a CLI in Go Using Cobra - JetBrains Guide. (2024, May 24). Retrieved February 25, 2026, from https://www.jetbrains.com/guide/go/tutorials/creating-cli-in-go-cobra/
Documentation - The Go Programming Language. (n.d.). Retrieved February 25, 2026, from https://go.dev/doc/
Building a Network Command Line Interface in Go - TutorialEdge.net. (2018, July 29). Retrieved February 25, 2026, from https://tutorialedge.net/golang/building-a-cli-in-go/
Creating A CLI In Golang - DEV Community. (2021, April 17). Retrieved February 25, 2026, from https://dev.to/narasimmantech/creating-a-cli-in-golang-5g2d
Go Modules - Heroku Dev Center. (2025, December 10). Retrieved February 25, 2026, from https://devcenter.heroku.com/articles/go-modules
Understanding Go Modules for Beginners - TiDB. (2024, September 2). Retrieved February 25, 2026, from https://www.tidb.cloud/blog/understanding-go-modules-for-beginners
Learn Go by Building a Command Line Todo App | Barbarian Meets Coding. (2025, March 2). Retrieved February 25, 2026, from https://barbarianmeetscoding.com/blog/learn-go-by-building-a-command-line-todo-app
Installing Go: Let's get Started! - ByteSizeGo. (2024, September 12). Retrieved February 25, 2026, from https://bytesizego.com/installing-go
Download and install - The Go Programming Language. (n.d.). Retrieved February 25, 2026, from https://go.dev/doc/install
Install and configure Visual Studio Code for Go development - Go on Azure | Microsoft Learn. (2025, June 2). Retrieved February 25, 2026, from https://learn.microsoft.com/en-us/azure/developer/go/configure-visual-studio-code
Using Go Modules - The Go Programming Language. (2019, March 19). Retrieved February 25, 2026, from https://go.dev/blog/using-go-modules
Getting started with Go modules. Golang is still a fairly new… | by Niraj Fonseka | Medium. (2018, December 6). Retrieved February 25, 2026, from https://medium.com/@nirajfonseka/getting-started-with-go-modules-b3268007a3d3

Challenge

Write a complete, executable Go program in the editor.

Your program must:

  1. Declare itself as the main package.
  2. Import two packages from the standard library: fmt for formatting and printing, and time for time-related functions.
  3. Contain a main function as the entry point.
  4. Inside the main function, first print the exact string "Welcome! The current time is:" to the console.
  5. On the very next line, print the current time by calling the appropriate function from the time package.

Expected result

Your code should be a complete Go program that correctly imports and uses both the fmt and time packages to print a welcome message followed by the current time.

go
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("Welcome! The current time is:")
    fmt.Println(time.Now())
}
go

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.