Hello World: An Introduction to Go
22 January 2025 / 6 min read
In this blog post, we’ll briefly cover the history and current state of Go (or Golang) and then look at how you can get started with Go as a beginner.
Why Go was developed
Go was developed by three engineers - Robert Griesemer, Rob Pike and Ken Thompson - at Google and released in the year 2009.
Go as a language was designed to be simple and straightforward in its syntax, but still powerful enough to meet the demands of large, distributed systems.
The developers paid particular attention to
- concurrency (i.e. handling multiple tasks simultaneously)
- fast compilation
- minimalist but expressive syntax
Why Go is in demand
Thanks to its built-in support for concurrency, realized through goroutines and channels, Go is ideal for applications that require high parallelism and scalability.
Technologies like Docker and Kubernetes, which have revolutionized modern software development, were written in Go - a clear sign of the language’s power and efficiency.
Go code compiles into a self-contained binary, allowing it to run in any environment without the need for a virtual machine. This makes it more efficient than languages like Java or Kotlin, which rely on the Java Virtual Machine (JVM) to execute.
This makes Go a solid choice for developing microservices because Go’s self-contained binaries:
- simplify deployment,
- ensure consistent execution across diverse environments,
- reduce resource overhead compared to languages requiring external runtime environments like the JVM.
Thanks to these benefits, many well-known tech companies are increasingly using Go for their backend microservices. These include Google, Coinbase, Uber and many, many more.
Go is poised to play an increasingly important role in software engineering in the future, particularly excelling in areas like
- cloud computing,
- DevOps,
- backend microservices,
- distributed systems,
- networking tools,
- container orchestration,
- and AI/ML infrastructure.
Its simplicity, performance, and native support for concurrency make it ideal for building distributed and scalable systems, while its growing ecosystem and compatibility with modern workloads ensure it will remain pivotal in tackling emerging challenges across these domains.
Your first Go program
You can download and install Go locally by following the instructions on the official Go website.
You can verify if Go was installed by running go version
in your terminal which should give you a response like this:
go version go1.23.4 linux/amd64
Using our terminal, we’ll create a new directory and go inside:
mkdir helloworld-go
cd helloworld-go
Then we’ll create our very first Go module:
go mod init github.com/shrzkhn/helloworld-go
I used my own GitHub username here in the name of the module, but you should of course replace it with your own username.
This will initialize a module by creating a go.mod
file with content similar to this:
module github.com/shrzkhn/helloworld-go
go 1.23.4
A go.mod
file is a configuration file that defines the module’s path, its dependencies, and their versions, enabling Go’s module-based dependency management system.
It is similar to the package.json
in JavaScript or the composer.json
in PHP.
What is a Go module
In Go, a module is a collection of related Go source code that is managed and versioned together. A module contains at least one Go file and has a unique module identifier defined in the go.mod
file.
This file specifies the module name (usually - but not necessarily - based on a repository URL such as github.com/user/repo
to ensure uniqueness) and the dependencies used with their versions.
Go modules allow easy management of dependencies and help ensure consistent builds, especially when working with multiple versions of libraries.
Hello World
Now we will create a new file:
touch hello.go
And write our very first Go program inside:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
We can run this code in our terminal with go run
and get this output:
Hello World
The anatomy of a simple Go program
Let’s look at this Hello World program line by line:
package main
package
: This keyword defines the package name for the file. A package in Go is a way to organize and modularize code. It is similar to namespaces in Java or PHP. Every Go program starts execution from themain
package.main
: The name of this package. It indicates that this is the entry point of the program. If a Go file belongs to the main package, it must contain amain()
function, which is where the program starts executing.
import "fmt"
import
: This keyword is used to include other packages or libraries into the program so you can use their functions and features."fmt"
: This is the name of the imported package. Thefmt
package provides functions for formatted I/O (input/output), such as printing text to the console or scanning input from the user.
func main()
func
: This keyword declares a function. Functions in Go are reusable blocks of code designed to perform a specific task.main()
: The name of the function. Themain()
function is special in Go because it acts as the entry point for the program. When the program is executed, themain()
function is run automatically. The paranthesis are empty because this function does not take any arguments.
fmt.Println("Hello World")
- A function provided by the
fmt
package. It prints the specified string to the console, followed by a newline.fmt
: Refers to the package name.Println
: A function within the fmt package that prints its arguments and adds a newline at the end.
Useful commands
Let’s look at 3 commands which are useful for a beginner when starting out with Go.
go run
You already met this one. This command compiles the code to an executable in a temporary location, runs it, and then cleans up after execution. It is useful for quickly testing or running a Go program without creating a binary.
Example: go run main.go
go build
This compiles the Go source code into an executable binary without running it, creating a standalone executable to distribute or use later. In our code up there, you could use it like this:
go build hello.go
This will create a binary file named hello
(or hello.exe
if you are on Windows). Let’s execute it:
./hello
This outputs Hello World.
With the flag -o
you can specify a different name.
go build -o app hello.go
./app
This will again output Hello World.
go fmt
This command formats the Go source code in a standard way, ensuring consistent formatting of code across a project according to Go’s style guidelines.
You can format only one file:
go fmt main.go
Or you can format all files in the current module:
go fmt ./...
Let’s try it out and intentionally remove the tabs and blank lines in our hello.go
file:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
Now if we run go fmt
it looks clean again:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
Often IDE’s like VS Code or GoLand will automatically keep formatting as you save the .go
files, but it is also good practice to run go fmt
before you run go build
.