TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Go

Learn the Go Programming Language: Start Here

This tutorial will teach you how to set up a Go programming environment and write your first Golang program.
Mar 14th, 2024 5:00pm by
Featued image for: Learn the Go Programming Language: Start Here

The Go programming language (aka Golang) is a versatile programming language that is used for building low-level infrastructure, web applications and services, cloud native applications, distributed systems, networked applications, concurrent processing tasks, networking tools, proxies and command-line tools, and also works well for containers, IoT and embedded systems.

The main reason why Go is so popular is its speed. Developers who use Golang as their go-to option will tell you that it has one of the fastest compile times of any language.

Unlike Python, Go is a compiled language that is statically typed, explicit and modeled after the C programming language. Even so, Go was inspired by the simplicity of Python. And because Go uses goroutines (a lightweight execution thread and function that executes concurrently with the rest of a Go program), it can produce programs that are able to run multiple processes at the same time (concurrency).

Go was created in 2007 by Google to eliminate some of the issues that they identified when developing software internally (such as slow build times and better concurrency). Google also wanted to create a language that would help make the development process more productive and scalable. What they ultimately wanted was a simple, efficient and easy-to-use programming language.

Hence, Golang.

Pros and Cons of Golang

PRO CON
Simplicity and easy to learn and use Allows for sloppy coding practices
Concurrency Lack of libraries
Fast compilation Static typing can create less flexible code
Built-in testing (with good error messages) Comparatively small community
Garbage collection While compile time is very fast, compiled code isn’t the fastest.
Can use all CPU cores efficiently No inheritance
Best-in-class documentation
Fairly powerful profiling tools
Eliminates the distinction between synchronous and asynchronous code

Now that you have an understanding as to why Go was created, let’s get into how it’s used.

As with everything in the world of programming, if you’re not familiar with the language, it’s always best to start from the beginning. With Go, the beginning starts with installation. I’m going to show you how to get Go up and running on a daily build of Ubuntu Linux 24.04. If you use a different Linux distribution (or macOS or Windows), you’ll need to alter the installation instructions as needed.

Installing Go on Ubuntu Linux

Before you can use Go, it must be installed. There are several methods for installing Go, but we’ll stick with the two primary paths. Since we’re dealing with Ubuntu, one route to installing Go is via Snap, which is done with this command:


If you’re not a fan of Snap packages, you can instead install it via apt and the standard repositories with this command:


You can verify the installation was successful with this command:


You should see something like this in the output:


Congratulations, the Go language is ready to … go!

Your First Go Application

As you’ve probably predicted, the first application we’ll create is “Hello, World” — because that’s where every language tutorial should begin.

Create a new directory to house your Go projects:


Next, change into that directory:


Now create the “Hello, World” file with this command:


In that file, paste the following:


What does it all mean? Let’s break it down, line by line.

  • package main: This line calls the main package, which is included with Go.
  • import “fmt”: This imports the package that is used to format basic strings, values, inputs, and outputs.
  • func main(): This is a special type of function, and it is the entry point of the executable programs. Every executable program must contain this function.
  • fmt.Println(“Hello, New Stack”): This uses the default formats for its operands and writes to standard output.

Save and close the file.

To compile and run the application, issue this command:


The output will be:


You’ll notice that Go does not create an executable file. Unlike languages like Python, Go includes the ability to generate an executable application from your .go file. Sticking with our hello.go file, the command for this would be:


After you run this, you’ll find a new file in the directory called hello. To run the application, issue the command:


You should see the same output as you did before. The big difference is that you can copy that application to any machine (as long as it’s running the same OS). If you want to cross-compile, that’s when you’ll run into trouble. Why? Because the default go build command assumes you’re building for the same OS and architecture.

Let’s say, however, you want to build the Hello app on Linux but want it to run on a macOS machine. For that, you’ll have to add a few options to the command. The options are as follows:

  • GOOS: defines the OS to be compiled for
  • GOARCH: defines the architecture to be compiled for

For instance, say you want to compile the application to run on an Intel-based MacOS machine. That command would be:


For an Apple Silicon Mac, that command would be:


To list all of the operating systems and architectures, issue this command:


You should see a full list of all the combinations that are available for cross-compiling your Go app.

Congratulations on taking your first steps with the Go language. Next time around we’ll continue by digging a bit deeper into the language.

Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.