DEV Community

DigitalOcean for DigitalOcean

Posted on • Originally published at

How to Code in Go (Tutorial Series)

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.

How To Install Go and Set Up a Local Programming Environment on Ubuntu 18.04

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.

How To Install Go and Set Up a Local Programming Environment on macOS

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 macOS.

How To Install Go and Set Up a Local Programming Environment on Windows 10

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.

How To Write Your First Program in Go

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.

Understanding the GOPATH

This article will walk you through understanding what the GOPATH is, how it works, and how to set it up. This is a crucial step for setting up a Go development environment, as well as understanding how Go finds, installs, and builds source files.

How To Write Comments in Go

Comments are lines that exist in computer programs that are ignored by compilers and interpreters. Including comments in programs makes code more readable for humans as it provides some information or explanation about what each part of a program is doing. In this article, you'll learn how to work with comments in Go.

Understanding Data Types in Go

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.

An Introduction to Working with Strings in Go

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.

How To Format Strings in Go

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.

An Introduction to the Strings Package in Go

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

How To Use Variables and Constants in Go

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.

How To Convert Data Types in Go

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.

How To Do Math in Go with Operators

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.

Understanding Boolean Logic in Go

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.

Understanding Maps in Go

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.

Understanding Arrays and Slices in Go

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.

Handling Errors in Go

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.

Creating Custom Errors in Go

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.

Handling Panics in Go

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.

Importing Packages in Go

Making use of packages allows us to make our programs more robust and powerful. This tutorial will walk you through installing, importing, and aliasing packages.

How To Write Packages in Go

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

Understanding Package Visibility in Go

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.

How To Write Conditional Statements in Go

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.

How To Write Switch Statements in Go

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.

How To Construct For Loops in Go

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.

Using Break and Continue Statements When Working with Loops in Go

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.

How To Define and Call Functions in Go

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.

How To Use Variadic Functions in Go

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.

Understanding defer in Go

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.

Understanding init in Go

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.

Customizing Go Binaries with Build Tags

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.

Understanding Pointers in Go

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.

Defining Structs in Go

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.

Defining Methods in Go

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.

How To Build and Install Go Programs

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.

How To Use Struct Tags in Go

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.

How To Use Interfaces in Go

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.

Building Go Applications for Different Operating Systems and Architectures

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.

Using ldflags to Set Version Information for Go Applications

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.

How To Use the Flag Package in Go

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.

Top comments (2)

artoodeeto profile image

is it good to learn Go for computer security? just a side question. thank you for this article. :)