Getting Started with Go

Enock Omondi

Go, also known as Golang, is an open-source programming language created by Google. Known for its simplicity, efficiency, and strong support for concurrency, Go is an excellent choice for building scalable applications, microservices, and more.

In this guide, we'll walk you through setting up Go and writing your first program.


Prerequisites

Before you start, ensure that you have:

Step 1: Installing Go

To install Go, follow these steps:

  1. Visit the official Go website.
  2. Download the installer for your operating system.
  3. Run the installer and follow the prompts.

To verify the installation:

$ go version

You should see the installed version of Go.

Step 2: Setting Up Your Environment

  1. Create a directory for your Go projects:
$ mkdir go-projects
$ cd go-projects
  1. Set up your workspace by defining the GOPATH. Add the following to your shell profile (.bashrc, .zshrc, or equivalent):
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Reload your shell configuration:

$ source ~/.bashrc

Step 3: Writing Your First Go Program

Create a new file named main.go in your project directory:

To run your program:

$ go run main.go

You should see:

Hello, World!

Step 4: Compiling and Running Your Program

Go allows you to compile your code into a standalone binary:

$ go build main.go
$ ./main

The compiled binary can be executed directly without needing the Go runtime installed on the target system.

Step 5: Exploring Go Modules

Go uses modules for dependency management. To initialize a new module:

$ go mod init your-module-name

This creates a go.mod file, which tracks your project's dependencies.

To add external packages, use:

$ go get <package-name>

Conclusion

Congratulations! You've taken your first steps with Go. From here, you can explore more advanced topics, such as:

To continue learning, check out the official Go documentation and experiment with building small projects.


Happy coding!