Skip to main content
Top

2022 | Book

Pro Go

The Complete Guide to Programming Reliable and Efficient Software Using Golang

insite
SEARCH

About this book

Best-selling author Adam Freeman explains how to get the most from Go, starting from the basics and building up to the most advanced and sophisticated features. You will learn how Go builds on a simple and consistent type system to create a comprehensive and productive development experience that produces fast and robust applications that run across platforms.

Go, also known as Golang, is the concise and efficient programming language designed by Google for creating high-performance, cross-platform applications. Go combines strong static types with simple syntax and a comprehensive standard library to increase programmer productivity, while still supporting features such as concurrent/parallel programming.

Each topic is covered in a clear, concise, no-nonsense approach that is packed with the details you need to learn to be truly effective. Chapters include common problems and how to avoid them.

What You Will Learn

Gain a solid understanding of the Go language and tools Gain in-depth knowledge of the Go standard library Use Go for concurrent/parallel tasks Use Go for client- and server-side development

Who This Book Is For

Experienced developers who want to use Go to create applications

Table of Contents

Frontmatter

Understanding the Go Language

Frontmatter
Chapter 1. Your First Go Application

The best way to get started with Go is to jump right in. In this chapter, I explain how to prepare the Go development environment and create and run a simple web application. The purpose of this chapter is to get a sense of what writing Go is like, so don’t worry if you don’t understand all the language features that are used. Everything you need to know is explained in detail in later chapters.

Adam Freeman
Chapter 2. Putting Go in Context

Go, often referred to as Golang, is a language originally developed at Google that has started to gain widespread use. Go is syntactically similar to C, but it has safe pointers, automatic memory management, and one of the most useful and well-written standard libraries I have encountered.

Adam Freeman
Chapter 3. Using the Go Tools

In this chapter, I describe the Go development tools, most of which were installed as part of the Go package in Chapter 1. I describe the basic structure of a Go project, explain how to compile and execute Go code, and show you how to install and use a debugger for Go applications. I also describe the Go linting and formatting tools.

Adam Freeman
Chapter 4. Basic Types, Values, and Pointers

In this chapter, I begin to describe the Go language, focusing on the basic data types before moving on to how they are used to create constants and variables. I also introduce the Go support for pointers. Pointers can be a source of confusion, especially if you are coming to Go from languages such as Java or C#, and I describe how Go pointers work, demonstrate why they can be useful, and explain why they are not to be feared.

Adam Freeman
Chapter 5. Operations and Conversions

In this chapter, I describe the Go operators, which are used to perform arithmetic, compare values, and create logical expressions that produce true/false results. I also explain the process of converting a value from one type to another, which can be done using a combination of built-in language features and facilities provided by the Go standard library. Table 5-1 puts the Go operations and conversions in context.

Adam Freeman
Chapter 6. Flow Control

In this chapter, I describe the Go features for controlling the flow of execution. Go supports keywords that are common in other programming languages, such as if, for, switch, etc., but each has some unusual and innovative features. Table 6-1 puts the Go flow control features in context.

Adam Freeman
Chapter 7. Using Arrays, Slices, and Maps

In this chapter, I describe the built-in Go collection types: arrays, slices, and maps. These features allow related values to be grouped and, just as with other features, Go takes a different approach to collections when compared with other languages. I also describe an unusual aspect of Go string values, which can be treated like arrays, but behave in different ways depending on how the elements are used. Table 7-1 puts arrays, slices, and maps in context.

Adam Freeman
Chapter 8. Defining and Using Functions

In this chapter, I describe Go functions, which allow code statements to be grouped together and executed when they are needed. Go functions have some unusual characteristics, the most useful of which is the ability to define multiple results. As I explain, this is an elegant solution to a common issue that functions present. Table 8-1 puts functions in context.

Adam Freeman
Chapter 9. Using Function Types

In this chapter, I describe the way that Go deals with function types, which is a useful—if sometimes confusing—feature that allows functions to be described consistently and in the same way as other values. Table 9-1 puts function types in context.

Adam Freeman
Chapter 10. Defining Structs

In this chapter, I describe structs, which is how custom data types are defined in Go. I show you how to define new struct types, describe how to create values from those types, and explain what happens when values are copied. Table 10-1 puts structs in context.

Adam Freeman
Chapter 11. Using Methods and Interfaces

In this chapter, I describe the Go support for methods, which can be used to provide features for structs and to create abstraction through interfaces. Table 11-1 puts these features in context.

Adam Freeman
Chapter 12. Creating and Using Packages

Packages are the Go feature that allows projects to be structured so that related functionality can be grouped together, without the need to put all the code into a single file or folder. In this chapter, I describe how to create and use packages and how to use packages developed by third parties. Table 12-1 puts packages in context.

Adam Freeman
Chapter 13. Type and Interface Composition

In this chapter, I explain how types are combined to create new features. Go doesn’t use inheritance, which you may be familiar with in other languages and instead relies on an approach known as composition. This can be difficult to understand, so this chapter describes some features covered in earlier chapters to set a solid foundation on which to explain the composition process. Table 13-1 puts type and interface composition in context.

Adam Freeman
Chapter 14. Using Goroutines and Channels

Go has excellent support for writing concurrent applications, using features that are simpler and more intuitive than any other language I have used. In this chapter, I describe the use of goroutines, which allow functions to be executed concurrently, and channels, through which goroutines can produce results asynchronously. Table 14-1 puts goroutines and channels in context.

Adam Freeman
Chapter 15. Error Handling

This chapter describes the way that Go deals with errors. I describe the interface that represents errors, show you how to create errors, and explain the different ways they can be handled. I also describe panicking, which is how unrecoverable errors are handled. Table 15-1 puts error handling in context.

Adam Freeman

Using the Go Standard Library

Frontmatter
Chapter 16. String Processing and Regular Expressions

In this chapter, I describe the standard library features for processing string values, which are needed by almost every project and which many languages provide as methods defined on the built-in types. But even though Go defines these features in the standard library, a complete set of functions is available, along with good support for working with regular expressions. Table 16-1 puts these features in context.

Adam Freeman
Chapter 17. Formatting and Scanning Strings

In this chapter, I describe the standard library features for formatting and scanning strings. Formatting is the process of composing a new string from one or more data values, while scanning is the process of parsing values from a string. Table 17-1 puts formatting and scanning strings in context.

Adam Freeman
Chapter 18. Math Functions and Data Sorting

In this chapter, I describe two sets of features. First, I describe the support for performing common mathematical tasks, including generating random numbers. Second, I describe the features for sorting the elements in a slice so they are in order. Table 18-1 puts the math and sorting features in context.

Adam Freeman
Chapter 19. Dates, Times, and Durations

In this chapter, I describe the features provided by the time package, which is the part of the standard library responsible for representing moments in time and durations. Table 19-1 puts these features in context.

Adam Freeman
Chapter 20. Reading and Writing Data

In this chapter, I describe two of the most important interfaces defined by the standard library: the Reader and Writer interfaces. These interfaces are used wherever data is read or written, which means that any source or destination for data can be treated in much the same way so that writing data to a file, for example, is just the same as writing data to a network connection. Table 20-1 puts the features described in this chapter in context.

Adam Freeman
Chapter 21. Working with JSON Data

In this chapter, I describe the Go standard library support for the JavaScript Object Notation (JSON) format. JSON has become the de facto standard for representing data, largely because it is simple and works across platforms. See http://json.org for a concise description of the data format if you have not encountered JSON before. JSON is often encountered as the data format used in RESTful web services, which I demonstrate in Part 3. Table 21-1 puts the JSON features in context.

Adam Freeman
Chapter 22. Working with Files

In this chapter, I describe the features that the Go standard library provides for working with files and directories. Go runs on multiple platforms, and the standard library takes a platform-neutral approach so that code can be written without needing to understand the file systems used by different operating systems. Table 22-1 puts working with files in context.

Adam Freeman
Chapter 23. Using HTML and Text Templates

In this chapter, I describe standard library packages that are used to produce HTML and text content from templates. These template packages are useful when generating large amounts of content and have extensive support for generating dynamic content. Table 23-1 puts the HTML and text templates in context.

Adam Freeman
Chapter 24. Creating HTTP Servers

In this chapter, I describe the standard library support for creating HTTP servers and processing HTTP and HTTPS requests. I show you how to create a server and explain the different ways in which requests can be handled, including form requests. Table 24-1 puts HTTP servers in context.

Adam Freeman
Chapter 25. Creating HTTP Clients

In this chapter, I describe the standard library features for making HTTP requests, allowing applications to make use of web servers. Table 25-1 puts HTTP requests in context.

Adam Freeman
Chapter 26. Working with Databases

In this chapter, I describe the Go standard library for working with SQL databases. These features provide an abstract representation of the capabilities offered by a database and rely on driver packages to deal with the implementation of a specific database.

Adam Freeman
Chapter 27. Using Reflection

In this chapter, I describe the Go support for reflection, which allows an application to work with types that are not known when the project is compiled, which is useful for creating APIs that will be used by other projects, for example. You can see extensive use of reflection in Part 3, where I create a custom web application framework. In this situation, the code in the application framework doesn’t know anything about the data types that will be defined by the applications it is used to run and has to use reflection to get information about those types and to work with values created from them.

Adam Freeman
Chapter 28. Using Reflection, Part 2

In addition to the basic features described in the previous chapter, the reflect package provides additional features that are useful when working with a specific kind of type, such as maps or structs. In the sections that follow, I describe these features and demonstrate their use. Some of the methods and functions described are used with more than one type, and I have listed them several times for quick reference. Table 28-1 summarizes the chapter.

Adam Freeman
Chapter 29. Using Reflection, Part 3

In this chapter, I complete the description of the Go support for reflection, which I started in Chapter 27 and continued in Chapter 28 . In this chapter, I explain how reflection is used for functions, methods, interfaces, and channels. Table 29-1 summarizes the chapter.

Adam Freeman
Chapter 30. Coordinating Goroutines

In this chapter, I describe the Go standard library packages with features that are used to coordinate goroutines. Table 30-1 puts the features described in this chapter in context.

Adam Freeman
Chapter 31. Unit Testing, Benchmarking, and Logging

In this chapter, I finish describing the most useful standard library packages with unit testing, benchmarking, and logging. The logging features are fine, if a little basic, and there are plenty of third-party packages available for directing log messages to different destinations. The testing and benchmarking features are integrated into the go command, but, as I explain, I am not enthusiastic about either feature. Table 31-1 summarizes the chapter.

Adam Freeman

Applying Go

Frontmatter
Chapter 32. Creating a Web Platform

In this chapter, I begin development on a custom web application platform, which I continue in Chapters 33 and 34 . In Chapters 35 – 38 , I use this platform to create an application named SportsStore, which I include in just about all of my books in some form.

Adam Freeman
Chapter 33. Middleware, Templates, and Handlers

In this chapter, I continue development of the web application platform started in Chapter 32 , adding support for handling HTTP requests.

Adam Freeman
Chapter 34. Actions, Sessions, and Authorization

In this chapter, I complete the development of the custom web application platform started in Chapter 32 and continued in Chapter 33 .

Adam Freeman
Chapter 35. SportsStore: A Real Application

In this chapter, I start the development of an application named SportsStore, which is an online store for sports products. This is an example that I include in many of my books, allowing me to demonstrate how the same set of features is implemented in different languages and frameworks.

Adam Freeman
Chapter 36. SportsStore: Cart and Database

In this chapter, I continue the development of the SportsStore application, adding support for a shopping cart and introducing a database to replace the temporary repository I created in Chapter 35 .

Adam Freeman
Chapter 37. SportsStore: Checkout and Administration

In this chapter, I continue the development of the SportsStore application by adding a checkout process and starting work on the administration features.

Adam Freeman
Chapter 38. SportsStore: Finishing and Deployment

In this chapter, I complete the development of the SportsStore application and prepare it for deployment.

Adam Freeman
Backmatter
Metadata
Title
Pro Go
Author
Adam Freeman
Copyright Year
2022
Publisher
Apress
Electronic ISBN
978-1-4842-7355-5
Print ISBN
978-1-4842-7354-8
DOI
https://doi.org/10.1007/978-1-4842-7355-5

Premium Partner