Check out all our Tutorial Series ->

Tutorial Series

How To Code in Go

How To Code in Go

Introduction

Go (or GoLang) is a modern programming language originally developed by Google that uses high-level syntax similar to scripting languages. It is popular for its minimal syntax and innovative handling of concurrency, as well as for the tools it provides for building native binaries on foreign platforms.

Summary View
detailed View

Series Tutorials

Book

How To Code in Go eBook

Published on June 12, 2020

This book is designed to introduce you to writing programs with the Go programming language. You’ll learn how to write useful tools and applications that can run on remote servers, or local Windows, macOS, and Linux systems for development.

This book is based on the How To Code in Go tutorial series found on DigitalOcean Community.

Tutorial

Go is a programming language that was designed for fast compilation, ease of programming, and efficient execution in production. This tutorial will guide you through installing and configuring a programming workspace with Go via the command line on Ubuntu 18.04.

Tutorial

Go is a programming language that was designed for fast compilation, ease of programming, and efficient execution in production. This tutorial will guide you through installing Go on macOS and running your first program.

Tutorial

Go is a programming language that was designed for fast compilation, ease of programming, and efficient execution in production. This tutorial will guide you through installing and configuring a programming workspace with Go via the command line on Windows 10.

Tutorial

The “Hello, World!” program is a classic and time-honored tradition in computer programming. It’s a simple and complete first program for beginners, and it’s a good way to make sure your environment is properly configured. This tutorial will walk you through creating this program in Go.

Tutorial

How To Write Comments in Go

Updated on January 19, 2023

Almost all programming languages have a syntax for adding comments to code, and Go is no exception. Comments are ignored by the compiler, but they add invaluable context that helps your collaborators—and your future self—to avoid pitfalls and write more maintainable code.

In this tutorial, we’ll look at some examples of helpful comments from a few Go packages to show not only how comments look, but what they should convey.

Tutorial

Understanding Data Types in Go

Published on May 1, 2019

Data types specify the kinds of values that particular variables will store when you are writing a program. The data type also determines what operations can be performed on the data. In this article, we will go over the important data types native to the Go programming language. Understanding some basic data types will enable you to write clearer code that performs efficiently.

Tutorial

This Go tutorial will go over the basics of working with strings, including how to create and print strings, concatenate and replicate strings, and store strings in variables.

Tutorial

How To Format Strings in Go

Published on April 23, 2019

In this tutorial, we’ll go over some of the ways we can work with Go strings to make sure that all output text is formatted correctly. Topics we will cover include: quotes, apostrophes, multiple lines, escape characters, and raw strings.

Tutorial

Go’s string package has several functions available to work with the string data type. These functions let us easily modify and manipulate…

Tutorial

Variables are an important programming concept to master. They are symbols that stand in for a value you’re using in a program. This tutorial will cover some variable basics and best practices for using them within the Go programs you create.

Tutorial

How To Convert Data Types in Go

Published on May 9, 2019

In Go, data types are used to classify one particular type of data, determining the values that you can assign to the type and the operations you can perform on it. When programming, there are times you will need to convert values between types in order to manipulate values in a different way. This tutorial will guide you through converting numbers and strings, as well as provide examples to help familiarize yourself with different use cases.

Tutorial

How To Do Math in Go with Operators

Published on May 15, 2019

Effectively performing mathematical operations in programming is an important skill to develop because of how frequently you’ll work with numbers. This tutorial will review operators that we can use with the integer and float data types in Go.

Tutorial

Understanding Boolean Logic in Go

Published on May 13, 2019

The Boolean data type can be one of two values, either True or False. We use Booleans in programming to make comparisons and to control the flow of the program. In this tutorial, we’ll go over the basics you’ll need to understand how Booleans work in Go, including Boolean comparison and logical operators, and truth tables.

Tutorial

Understanding Maps in Go

Published on April 23, 2019

Most modern programming languages have the concept of a dictionary or a hash type. These types are commonly used to store data in pairs with a key that maps to a value. In Go, the map is what most programmers would think of as the dictionary type. It maps keys to values, making key-value pairs that are a useful way to store data in Go. Understand how Go maps work in this article.

Tutorial

Understanding Arrays and Slices in Go

Published on July 16, 2019

This article will cover the array and slice data structures in the Go Programming language, which will provide you with the necessary information to make the appropriate choice when choosing between them. You’ll also review the most common ways to declare and work with both arrays and slices. The tutorial will first provide a description of arrays and how to manipulate them, followed by an explanation of slices and how they differ.

Tutorial

Handling Errors in Go

Published on July 23, 2019

Robust code needs to react correctly to unexpected circumstances like bad user input, faulty network connections, and failing disks. Error handling is the process of identifying when your program is in an unexpected state, and taking steps to record diagnostic information for later debugging.

Tutorial

Creating Custom Errors in Go

Published on September 5, 2019

When communicating more complicated error information to your users, or to your future self when debugging, sometimes these two mechanisms are not enough to adequately capture and report what has happened. To convey this more complex error information we can implement the standard library interface type, error, to get more functionality.

Tutorial

Handling Panics in Go

Published on October 3, 2019

Panics are unforeseeable errors that will spontaneously terminate and exit a running Go program. Common mistakes are often responsible for creating panics. In this tutorial, we’ll examine a few ways that common operations can produce panics in Go, and we’ll also see ways to avoid those panics. We’ll also use defer statements along with the recover function to capture panics before they have a chance to unexpectedly terminate our running Go programs.

Tutorial

Importing Packages in Go

Updated on February 1, 2023

The ability to borrow and share code across different projects is foundational to any widely-used programming language—and the entire open-source community. In Go, the basic unit of reusable code is called a package. In this tutorial, you will write you short programs—one that imports a standard library package, and one that imports a third-party package. You will also write an extended program comparing two similar packages, and also use the goimports tool to see how to format your import.

Tutorial

How To Write Packages in Go

Updated on August 17, 2019

Go packages are directories that consist of Go code. This tutorial will guide you through writing Go packages for use within other programming files.

Tutorial

Understanding Package Visibility in Go

Published on September 25, 2019

Visibility in the Go programming language means the file space from which a package or other construct can be referenced. In this article, you will learn how to control package visibility, as well as how to protect parts of your code that should only be used inside your package. To do this, we will create a basic logger to log and debug messages, using packages with varying degrees of item visibility.

Tutorial

How To Write Conditional Statements in Go

Published on September 5, 2019

Conditional statements are part of every programming language. With conditional statements, we can have code that sometimes runs and at other times does not run, depending on the conditions of the program at that time. This tutorial will take you through writing conditional statements in the Go programming language.

Tutorial

How To Write Switch Statements in Go

Published on October 24, 2019

switch is an alternative conditional statement useful for communicating actions taken by your Go programs when presented with different options. Everything we can write with the switch statement can also be written with if statements. We’ll look at a few examples of what the switch statement can do, the if statements it replaces, and where it’s most appropriately applied.

Tutorial

How To Construct For Loops in Go

Published on September 13, 2019

In the Go programming language, a for loop implements the repeated execution of code based on a loop counter or loop variable. In this tutorial, you will learn how Go’s for loop works, including the three major variations of its use: ForClause, Condition, and RangeClause. We’ll start by showing how to create different types of for loops, followed by how to loop through sequential data types in Go. We’ll end by explaining how to use nested loops.

Tutorial

Using for loops in Go allow you to automate and repeat tasks in an efficient manner. Learning how to control the operation and flow of loops will allow for customized logic in your program. You can control your loops with the break and continue statements.

Tutorial

How To Define and Call Functions in Go

Published on September 11, 2019

A function is a section of code that, once defined, can be reused. Functions are used to make your code easier to understand by breaking it into small, understandable tasks that can be used more than once throughout your program. In this tutorial, we’ll go over how to define your own functions to use in your coding projects.

Tutorial

How To Use Variadic Functions in Go

Updated on September 12, 2019

A variadic function is a function that accepts zero, one, or more values as a single argument. While variadic functions are not the common case, they can be used to make your code cleaner and more readable.

Tutorial

Understanding defer in Go

Published on September 28, 2019

Go has many of the common control flow keywords found in other programming languages such as if, switch, for, etc. One keyword that isn’t found in most other programming languages is defer, and though it’s less common you’ll quickly see how useful it can be in your programs. In this article we will learn how to properly use the defer statement for cleaning up resources as well as several common mistakes that are made when using defer.

Tutorial

Understanding init in Go

Published on September 26, 2019

In Go, the predefined init() function sets off a piece of code to run before any other part of your package. This code will execute as soon as the package is imported, and can be used when you need your application to initialize in a specific state. In this tutorial, you’ll learn how init() is used for the setup and initialization of specific package variables, one time computations, and the registration of a package for use with another package.

Tutorial

Customizing Go Binaries with Build Tags

Published on October 1, 2019

In Go, a build tag, or a build constraint, is an identifier added to a piece of code that determines when the file should be included in a package during the build process. This allows you to build different versions of your Go application from the same source code and to toggle between them in a fast and organized manner. In this article, you will use build tags in Go to generate different executable binaries that offer Free, Pro, and Enterprise feature sets of a sample application.

Conceptual-article

Understanding Pointers in Go

Published on October 4, 2019

When writing software in Go you’ll be writing functions and methods. You pass data to these functions as arguments. Sometimes, the function needs a local copy of the data, and you want the original to remain unchanged. In this article, you will learn how to create and use pointers to share access to the memory space for a variable.

Tutorial

Defining Structs in Go

Published on October 24, 2019

Structs allow storing data from several variables in a single entity with one name. They allow Go developers to describe the world in which a Go program operates. Instead of reasoning about strings describing a Street, City, or a PostalCode, structs allow us to instead talk about an Address. They also serve as a natural nexus for documentation. Structs can be defined and used in a few different ways, which are discussed in this tutorial.

Tutorial

Defining Methods in Go

Published on October 31, 2019

Methods are Go functions that operate on instances of a specific type. Methods allow you to communicate not only what the data is, but also how that data should be used. Methods are the core concept that makes Go interfaces possible.

Tutorial

How To Build and Install Go Programs

Updated on April 27, 2022

In Go, distributing or deploying your application requires you to build your code into a shareable binary executable. To do this, you can use the Go toolchain to build and install your program. In this tutorial, you will use the Go toolchain to run, build, and install a sample Hello, World! program, allowing you to use, distribute, and deploy future applications effectively.

Tutorial

How To Use Struct Tags in Go

Updated on June 23, 2022

Struct tags are small pieces of metadata attached to fields of a struct that provide instructions to other Go code that works with the struct. When you read information from systems such as databases, or APIs, you can use struct tags to control how this information is assigned to the fields of a struct.

Tutorial

How To Use Interfaces in Go

Published on November 6, 2019

In this article, we will learn how to compose custom types that have common behaviors, which will allow us to reuse our code. You’ll also learn how to implement interfaces for your own custom types that will satisfy interfaces defined from another package.

Tutorial

Go supports cross-platform compiling by building support for multiple platforms directly into the go build tool. By using the GOOS and GOARCH environment variables and build tags, you can control which OS and architecture your final binary is built for. In this tutorial, you will build binaries for multiple operating systems and system architectures on your own system.

Tutorial

In this tutorial, you will use the Go flag -ldflags to change the value of variables at build time and introduce your own dynamic information into a binary, using a sample application that prints version information to the screen. This passes a flag to the underlying Go toolchain linker, cmd/link, that allows you to change the values of imported packages at build time from the command line.

Tutorial

How To Use the Flag Package in Go

Published on November 8, 2019

In this tutorial you’ll explore various ways to use the flag package to build different kinds of command-line utilities. You’ll use a flag to control program output, introduce positional arguments where you mix flags and other data, and then implement sub-commands.

Tutorial

How to Use Go Modules

Published on October 27, 2021

A Go module commonly consists of one project or library and contains a collection of Go packages that are then released together. With Go modules, you can put code where you want and specify dependencies for each module.

Tutorial

How to Distribute Go Modules

Published on November 18, 2021

With Go, developers can use ready-made libraries from other developers and publish their own for others to use. In this tutorial, you’ll create and publish a new module, learn about semantic versioning, and publish a semantic version of your module.

Tutorial

Many Go modules are open-source, which means they can be freely accessed and used. However, sometimes you need to make a module private. In this tutorial, you will publish a private Go module, set up authentication to access a private module, and use a private Go module in a project.

Tutorial

To run programs faster, a programmer needs to design their programs to run at the same time. Two features in Go, goroutines and channels, make concurrency easier when used together. Goroutines solve the difficulty of setting up and running concurrent code in a program, and channels solve the difficulty of safely communicating between the code running concurrently.

Tutorial

In Go 1.13, new features were added to make it easier to add extra information to errors: fmt.Errorf, errors.Is, and errors.As. In this tutorial, you’ll create a program that uses these functions to include additional information in errors returned from your functions, and then create your own custom error struct that supports the wrapping and unwrapping functionality.

Tutorial

How To Use Dates and Times in Go

Published on February 2, 2022

Date and time values show up everywhere in modern software. Learn to use Go’s time package to get the current local time of your computer and customize the format. You can also translate the date and time values between two time zones, as well as add or subtract time values to determine the interval between two times.

Tutorial

How To Use Contexts in Go

Published on February 16, 2022

When developing a large application, it can be helpful for a function to know more about the environment it’s being executed in aside from the information needed for a function to work on its own. To help with this, Go includes a context package in its standard library. Learn to create a Go program that uses a context within a function. You’ll also store additional data in the context and retrieve it from another function. Finally, you’ll explore methods for ending a context.

Tutorial

How To Use JSON in Go

Published on March 28, 2022

Programs need to store data their own data and communicate with each other. JSON is a popular way to do both. In this tutorial, you will use Go’s encoding/json package to enode and decode JSON data so that you can save your own JSON data and interact with APIs.

Tutorial

How To Make an HTTP Server in Go

Published on April 21, 2022

In this tutorial, you will create an HTTP server using Go’s standard library and then expand your server to read data from the request’s query string, the body, and form data. You’ll also update your program to respond to the request with your own HTTP headers and status codes.

Tutorial

How To Make HTTP Requests in Go

Published on April 26, 2022

In this tutorial, you will create a program that makes several types of HTTP requests to an HTTP server. First, you will make a GET request using the default Go HTTP client. Then, you will enhance your program to make a POST request with a body. Finally, you will customize your POST request to include an HTTP header and add a timeout that will trigger if your request takes too long.

Tutorial

How To Use Generics in Go

Published on June 3, 2022

In this tutorial, you will use Go’s generics feature to interact with a deck of cards. You’ll begin by creating a deck that uses interface{} to interact with the cards, and then you will update it to use generic types. After those updates, you will add a second type of card to your deck using generics, and then you will update your deck to constrain its generic type to only support card types. Finally, you will create a function that uses your cards and supports generic types.

Tutorial

How To Use Templates in Go

Published on February 2, 2023

Do you need to present some data in well-formatted output, textual reports, or HTML pages? You can do that with Go templates. Any Go program can use the text/template or html/template package—both included in the Go standard library—to present data neatly. This tutorial will show you how to use both template packages.

Check out all our Tutorial Series

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.