What Is the Go Programming Language?

Author: Published

In brief: Go is a general-purpose programming language created at Google and developed as an open-source project. It is most commonly used to build server-side services, command-line tools and infrastructure software. It combines relatively simple syntax, compilation to machine code and built-in concurrency mechanisms, which organise multiple tasks whose execution can overlap in time.

How does Go differ from other languages?

The language’s official name is Go, although you will also encounter the term “Golang” in search engines and conversations. It does not refer to a different version or a separate technology. Go is also unrelated to the board game of the same name.

Go is a statically typed language: data types are checked during compilation, before the program runs. The compiler converts source code into a program for a selected operating system and processor architecture. It also catches some errors early, such as an attempt to use text where a number is required.

The language automatically reclaims memory that the program no longer needs. This is handled by a mechanism called a garbage collector. Developers do not have to free every area of memory manually, but they should still monitor resource usage and close files and connections.

Go deliberately has a limited number of language constructs. It does not offer the traditional class inheritance found in Java; instead, it uses structures and interfaces, among other mechanisms, to organise data and behaviour. Errors are usually returned as values that the calling code should explicitly check.

What is Go used for?

Go is well suited to programs that handle networking, process requests and connect different systems. Its standard library includes support for HTTP, JSON, files and testing, among other things, so many basic tasks can be performed without an extensive set of add-ons.

Typical uses include:

For example, an online shop might have a separate service written in Go that retrieves shipment statuses from several carriers and provides a consistent response to the customer’s dashboard. Go is a sensible choice here because of its networking tools and support for concurrent operations. However, the language itself will not solve problems with external API limits, outages or faulty order logic.

How does concurrency work in Go?

The fundamental concurrency mechanism in Go is goroutines: lightweight tasks managed by the language’s runtime. Calling a function with the keyword go before it allows that function to run concurrently with other tasks. A goroutine is not a separate operating-system thread; the Go runtime schedules goroutines to run on threads.

Tasks can communicate using channels, a mechanism for passing values and synchronising work. Locks are also available to protect shared data. Using goroutines does not automatically eliminate the risk of deadlocks or unsafe simultaneous access to memory.

Suppose a program needs to check the availability of ten websites. With a sequential approach, it waits for one website to respond before querying the next. With concurrency, it can start several checks and wait for their results at the same time, with a defined connection limit and timeout.

Concurrency is not the same as parallelism. The former concerns how tasks are organised; the latter concerns their actual simultaneous execution, for example on several processor cores. More goroutines do not guarantee a faster program: the bottleneck may be the database, network bandwidth or the cost of synchronisation.

When should you choose Go, and when should you choose another language?

Go is a reasonable candidate when you are building a standalone network service or a tool that needs to be easy to run on servers. You can often deliver a single executable file without installing a separate interpreter. This is not an unconditional rule: using C libraries or external files can introduce additional dependencies.

Choosing another language may be more practical in the following situations:

These are guidelines, not a ranking of language quality. When choosing, consider the available libraries, your team’s experience and the requirements of the specific program.

How do you start learning Go?

Install the current stable version following the instructions on the Go project’s official website, checking the requirements for your operating system. Then verify the installation with go version and create a directory for your first project.

Save a file named main.go in it:

package main

import "fmt"

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

In the terminal, run go mod init example/hello to create a module, which is a unit for organising a project and managing its dependencies. The command go run . will compile and run the program, while go build . will create an executable file. Use go fmt ./... to format the code and go test ./... to run tests.

As your next exercise, build a program that checks the HTTP status of a single website. Add error handling and a timeout, and only then extend it to check several addresses concurrently.

See also: What Is 2FA?

Read also

Learn how PHP-FPM handles PHP requests, how it differs from a web server, and how to tune processes for traffic and hosting resources.
Learn how BIND, open-source DNS server software, handles domains, how its two roles differ and when running it yourself makes sense.
Learn what Poland’s REGON register and identification number mean, how they differ from NIP and KRS, and how to check a business’s details.