Go (или GoLang) — современный язык программирования, первоначально разработанный Google, который использует высокоуровневый синтаксис, как в скриптовых языках. Этот язык стал популярным благодаря минимальному синтаксису и инновационным механизмам параллельной работы, а также доступным инструментам для сборки оригинальных двоичных файлов на сторонних платформах.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Go’s string package has several functions available to work with the string data type. These functions let us easily modify and manipulate…
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Go packages are directories that consist of Go code. This tutorial will guide you through writing Go packages for use within other programming files.
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.
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.
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.
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 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
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.