Go Tutorial For Beginners

Welcome to the Go Tutorial for beginners at Go-Tutorial.com! This tutorial teaches you to program in the popular Go programming language, sometimes abbreviated as Golang. It’s written for beginners, but those with previous programming experience will feel right at home as well.

Can I use this Go tutorial as a complete beginner?

Yes, you can! Most Go tutorials, courses, and books assume you know at least one programming language. And that’s what sets this tutorial apart from the rest. I won’t assume prior knowledge, but I do assume you have a good understanding of how computers work.

In other words: this is not a Computer Science 101 type of tutorial, but you can use this Go tutorial to learn programming from scratch. You may need to look things up, and I will provide links to other resources when I feel those will help you better understand the topic.

So what is Go?

Go is a modern, open-source programming language that makes it easy to build simple, secure, scalable,
and efficient software. If you are familiar with other programming languages, you could say that it is closer to C than to Python. It’s a compiled language, meaning the Go compiler outputs OS-native, machine-specific binaries. Go borrows modern ideas and paradigms from other languages while maintaining simplicity. Its syntax resembles C: Go has similar control flow, data types, and pointers.

Unlike C, Go offers memory management through a garbage collector, avoiding common causes of errors seen in other low-level programming languages. Go has built-in support for concurrency based on so-called go routines. Together with channels, these Go routines help us create concurrent software without making hard-to-debug mistakes.

Professional programmers often use Go to build efficient, secure software. It’s great for building backend systems (servers) and command-line tools. However, Go is a true general-purpose language. It offers rich, built-in libraries for all kinds of tasks and has a built-in package manager that helps you fetch and use packages created by others.

12 reasons to choose Go over other languages

Perhaps they apply to you, or perhaps not. This is something you have to decide for yourself:

  1. Go is a statically typed language that catches errors at compile time instead of runtime. This can make debugging and development more efficient.
  2. Go has a simple and clean syntax. It focuses on simplicity and minimalism, which can help developers avoid complexity and write clean, readable code.
  3. Go is a compiled language, which means that it can be much faster than interpreted languages. It also means there’s less overhead and potentially less energy consumption compared to similar code in other languages.
  4. Go has built-in concurrency support, making it a good choice for building high-performance, scalable systems.
  5. Go has a strong and supportive community, with many open-source libraries and frameworks available to help developers build great applications.
  6. Go has excellent cross-compilation support, making it easy to build applications that run on multiple platforms.
  7. Go has built-in garbage collection, which helps to manage memory usage and avoid memory leaks.
  8. Go has a powerful standard library that provides many useful features and functions, reducing the need to rely on external libraries.
  9. Go is backed by a strong team of developers at Google, which ensures that the language is constantly being improved and updated with new features and enhancements.
  10. Go has a robust toolchain that includes tools for testing, profiling, formatting, documenting, and linting, which help improve code quality and avoid common mistakes.
  11. Go has a strict adherence to backward compatibility, which means that code written in older versions of the language will continue to work in newer versions without requiring any changes.
  12. Go has built-in support for working with Unicode strings, which makes it easy to write applications that can handle text in multiple languages.

Does it run everywhere?

Pretty much, yes! Go is an open-source project, and the compiler, libraries, and tools are freely available. Go runs on Windows, Linux, MacOS, FreeBSD, and OpenBSD. A Go program will often compile and run without modification on any platform.

How do I read this Go Tutorial?

This Go tutorial is currently being written. Each page on this website is a complete, contained article about a certain Go topic. In the menu(s), you can find links to the available articles. This Go tutorial contains interactive, runnable example code and in-page assignments, but I will also help you install a good IDE with support for Go (VS Code).

To learn Go, all you need to do is keep reading. I’ll introduce you to the language, starting on this page with the oh-so-famous “Hello World” program!

Hello, world! Let’s start this Go Tutorial.

Let’s kick off this Go tutorial and get our hands dirty! Below, you’ll find a code example of a “Hello World” program. For those with prior programming experience, this will look familiar. For the absolute beginner, this may look overwhelming.

Closely study the example and then read on so we can dissect it line by line:

You can run this example right from your browser; feel free to do so and if you dare, adapt the code to your liking. Or continue reading, to learn exactly what this code does and why it works!

Files

Go code is stored in plain text files with the .go extension. The file above is called hello.go. You’ll learn to create these files later; let’s focus on the language syntax first.

Packages

Go code is organized in packages. The main package, as declared one line 1, is a special package that Go recognizes as the entry point of a program. You’ll learn much more about packages later on.

Importing

In Go, an import is a statement that makes the code from one or more other packages available to your Go program. When you import a package, you can use the functions, types, and variables defined in that package in your own Go code. Import statements are placed at the top of a .go file, before any other code but after the package declaration.

One package, which is used a lot by Go programmers, is the fmt package. It contains several functions for formatting and printing text, such as the Println function we used in the example.

Functions

A function is, in its essence, a named section of code. Our file contains a function called main. Functions are defined with the func keyword, followed by a name and parentheses. Between these parentheses, we can put optional arguments; we will discuss arguments later.

Each main package needs a main() function. Go will look for it, and it’s the first thing that gets executed when running a Go program. Go will print an error if it’s not there: “function main is undeclared in the main package.” Only the main package requires this main() function.

Printing to screen

Inside the main() function, we call another function in the fmt package, called Println. Because Println exists in the fmt package, we need to include the package name to reach it. Unlike our main() function, this function does accept arguments. In this case, we give it a string. A string is a computer term for a piece of text. Strings are typically enclosed in quotation marks to mark the start and end of the string.

You may have noticed the little heart icon (♥). I put it in there on purpose to show that Go supports Unicode out of the box. Go will happily work with text in any spoken language that is supported by Unicode, and yes, that includes emojis.

Code formatting

Code formatting is the process of organizing and structuring your code consistently and predictably. This includes things like indentation, whitespace, and the placement of braces and other punctuation.

Those with previous programming experience will notice that, unlike other languages with similar syntax, Go doesn’t require semicolons to mark the end of a line. This helps to keep code clean and readable.

Go’s code formatting is something that we’ll dive into later. But in general, most Go code is formatted automatically according to predefined rules using a tool called gofmt, included in every Go installation. This ensures that all Go code looks the same, regardless of who wrote it. It makes it easier to read and understand and avoids lengthy discussions about the best way to format code.

Build and run code: continue this Go tutorial

So we have a file called hello.go, containing Go code that we understand. How do we run this code? You’ll learn this in the next article: How to build and run a Go program.

Not for you?

If, after this introduction, you feel that Go is not for you (yet), I completely understand! Although general purpose, Go is not as general purpose and beginner-friendly as Python.

So please don’t give up on learning to program. Instead, try my extensive Python tutorial. Python is one of the world’s most popular general-purpose programming languages. It’s easier to learn, and you can use it for anything: backend programming, web development, data science, quick and dirty scripting, machine learning, and more!