Book Image

Learning Go Programming

Book Image

Learning Go Programming

Overview of this book

The Go programming language has firmly established itself as a favorite for building complex and scalable system applications. Go offers a direct and practical approach to programming that let programmers write correct and predictable code using concurrency idioms and a full-featured standard library. This is a step-by-step, practical guide full of real world examples to help you get started with Go in no time at all. We start off by understanding the fundamentals of Go, followed by a detailed description of the Go data types, program structures and Maps. After this, you learn how to use Go concurrency idioms to avoid pitfalls and create programs that are exact in expected behavior. Next, you will be familiarized with the tools and libraries that are available in Go for writing and exercising tests, benchmarking, and code coverage. Finally, you will be able to utilize some of the most important features of GO such as, Network Programming and OS integration to build efficient applications. All the concepts are explained in a crisp and concise manner and by the end of this book; you would be able to create highly efficient programs that you can deploy over cloud.
Table of Contents (18 chapters)
Learning Go Programming
About the Author
About the Reviewers


Go is an open source programming language that lets programmers easily build reliable and scalable programs. It does this by offering a simple syntax which makes it fun to write correct and predictable code using concurrency idioms and a robust standard library.

Go has a large and active online community and there are several Go conferences that take place around the world yearly.  Starting with, you will find numerous places on the web that provide documentations, blogs, videos, and slides that cover a wide range of Go-related topics.  On GitHub, the story is no different; some of the best known projects that are driving the future of cloud computing, for instance, are written in Go with an ever growing list.

As you would expect, getting started with Go is simple, fast, and well documented.  However, “getting into” Go can be more challenging, especially for newcomers from other languages.  My first attempt at Go failed.  Even after reading the prescribed documentations and going through the tutorials, there was a gap in understanding driven by my own biases from previous programming experiences.  Months later I returned to Go and got into it.  This time I read the language specs, I read blogs, watch videos, and searched the web for any discussion that provided design motivations and in-depth explanations of the language.

Learning Go is a book intended to help new, and seasoned programmers alike, to get into the Go programming language.  With this book, I have attempted to write the book I would have like to have read when I was starting out with Go.  It distills the language specs, the documentations, the blogs, the videos, slides, and my own experiences of writing Go into content that carefully provides the right amount of depth and insights to help you understand the language and its design.

I hope that you enjoy it.

What this book covers

Chapter 1, A First Step in Go, the reader is introduced to Go at a high-level and take a tour of the features that have made the language a favorite among its adopters.

Chapter 2, Go Language Essentials, this chapter starts with a deeper exploration Go’s syntax and other language elements such as source files, variables, and operators.

Chapter 3, Go Control Flow, examines Go program control flow elements including if, loop, and switch statements.

Chapter 4, Data Types, introduces its readers to Go’s type system including detail about built-in types, type declaration, and conversion.

Chapter 5, Functions in Go, discusses the characteristics of the Go function type including definition, assignment, variadic parameters, and closures.

Chapter 6, Go Packages and Program Structures, introduces readers to the organization of functions as a logical grouping known as packages and programs.

Chapter 7, Composite Types, this chapter continues the discussion Go types by introducing the reader to Go’s composite types such as arrays, slices, maps, and structs.

Chapter 8, Methods, Interfaces, and Objects, introduces the reader to Go idioms and features that can be used to create and compose object structures.

Chapter 9, Concurrency, introduces the reader to the topics of writing concurrent programs in Go using language constructs such as goroutines and channels.

Chapter 10, Data IO in Go, covers the built-in interfaces and APIs to achieve streaming input, output, and encoding of data.

Chapter 11, Writing Networked Services, explores the Go’s standard library for creating connected applications using covering topics from low-level TCP protocols to HTTP an RPC.

Chapter 12, Code Testing, here readers are introduced to Go’s inherent support and tools for code testing and benchmarking.

What you need for this book

To follow the examples in this book, you will need Go version 1.6 or later.  Go supports architectures including AMD64, x386, and ARM running the following operating systems:

  • Windows XP (or later)

  • Mac OSX 10.7 (or later)

  • Linux 2.6 (or later)

  • FreeBSD 8 (or later)

Who this book is for

If you have prior exposure to programming and are interested learning the Go, this book is designed for you. While it assumes that you are familiar with concepts such as variables, data types, arrays, methods, and functions, the book is designed to allow you to follow chapter by chapter or skip around to the topics you want to learn about.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Save the source code in a file called helloworld.go anywhere inside your GOPATH."

A block of code is set as follows:

package main
import "fmt"
func main() {
  fmt.Println("Hello, World!")

Any command-line input or output is written as follows:

$> go version
go version go1.6.1 linux/amd64

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "If all goes well, you should see the message Hello, World! output on your screen.."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at We also have other code bundles from our rich catalog of books and videos available at Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.