Book Image

Go: Design Patterns for Real-World Projects

By : Vladimir Vivien, Mario Castro Contreras, Mat Ryer
Book Image

Go: Design Patterns for Real-World Projects

By: Vladimir Vivien, Mario Castro Contreras, Mat Ryer

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 lets programmers write correct and predictable code using concurrency idioms and a full-featured standard library. This practical guide is full of real-world examples to help you get started with Go in no time at all. You’ll start by understanding the fundamentals of Go, then get a detailed description of the Go data types, program structures, and Maps. After that, you’ll learn how to use Go concurrency idioms to avoid pitfalls and create programs that are exact in expected behavior. Next, you will get familiar with the tools and libraries that are available in Go to write and exercise tests, benchmarking, and code coverage. After that, 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. Then you’ll start applying your skills to build some amazing projects in Go. You will learn to develop high-quality command-line tools that utilize the powerful shell capabilities and perform well using Go’s built-in concurrency mechanisms. Scale, performance, and high availability lie at the heart of our projects, and the lessons learned throughout the sections will arm you with everything you need to build world-class solutions. You will get a feel for app deployment using Docker and Google App Engine. Each project could form the basis of a start-up, which means they are directly applicable to modern software markets. With these skills in hand, you will be able to conquer all your fears of application development and go on to build large, robust and succinct apps in Go. This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products: 1. Learning Go Programming 2. Go Design Patterns 3. Go Programming Blueprints, Second Edition
Table of Contents (38 chapters)
Go: Design Patterns for Real-World Projects
Credits
Preface
Bibliography

Go tools


An early decision made by the Go core team was that all Go code should look familiar and obvious to everybody who speaks Go rather than each code base requiring additional learning in order for new programmers to understand it or work on it. This is an especially sensible approach when you consider open source projects, some of which have hundreds of contributors coming and going all the time.

There is a range of tools that can assist us in achieving the high standards set by the Go core team, and we will look at some of the tools in action in this section.

In your GOPATH location, create a new folder called tooling and create a new main.go file containing the following code verbatim:

package main 
import ( 
  "fmt" 
) 
func main() { 
  return 
  var name string 
  name = "Mat" 
  fmt.Println("Hello ", name) 
} 

The tight spaces and lack of indentation are deliberate as we are going to look at a very cool utility that comes with Go.

In a terminal, navigate to your new folder and run this:

go fmt -w

Note

At Gophercon 2014 in Denver, Colorado, most people learned that rather than pronouncing this little triad as format or f, m, t, it is actually pronounced as a word. Try saying it to yourself now: fhumt; it seems that computer programmers aren't weird enough without speaking an alien language to each other too!

You will notice that this little tool has actually tweaked our code file to ensure that the layout (or format) of our program matches Go standards. The new version is much easier to read:

package main  
import ( 
  "fmt" 
)  
func main() { 
  return 
  var name string 
  name = "Mat" 
  fmt.Println("Hello ", name) 
} 

The go fmt command cares about indentation, code blocks, unnecessary whitespace, unnecessary extra line feeds, and more. Formatting your code in this way is a great practice to ensure that your Go code looks like all other Go code.

Next, we are going to vet our program to make sure that we haven't made any mistakes or decisions that might be confusing to our users; we can do this automatically with another great tool that we get for free:

go vet

The output for our little program points out an obvious and glaring mistake:

main.go:10: unreachable code
exit status 1

We are calling return at the top of our function and then trying to do other things. The go vet tool has noticed this and points out that we have unreachable code in our file.

It isn't just silly mistakes like this that go vet will catch; it will also look for subtler aspects of your program that will guide you toward writing the best Go code you can. For an up-to-date list of what the vet tool will report on, check out the documentation at https://golang.org/cmd/vet/.

The final tool we will play with is called goimports, and it was written by Brad Fitzpatrick to automatically fix (add or remove) import statements for Go files. It is an error in Go to import a package and not use it, and obviously, trying to use a package without importing it won't work either. The goimports tool will automatically rewrite our import statement based on the contents of our code file. First, let's install goimports with this familiar command:

go get golang.org/x/tools/cmd/goimports

Update your program to import some packages that we are not going to use and remove the fmt package:

import ( 
  "net/http" 
  "sync" 
) 

When we try to run our program by calling go run main.go, we will see that we get some errors:

./main.go:4: imported and not used: "net/http"
./main.go:5: imported and not used: "sync"
./main.go:13: undefined: fmt

These errors tell us that we have imported packages that we are not using and missing the fmt package and that in order to continue, we need to make corrections. This is where goimports comes in:

goimports -w *.go

We are calling the goimports command with the -w write flag, which will save us the task of making corrections to all files ending with .go.

Have a look at your main.go file now, and note that the net/http and sync packages have been removed and the fmt package has been put back in.

You could argue that switching to a terminal to run these commands takes more time than just doing it manually, and you would probably be right in most cases, which is why it is highly recommended that you integrate the Go tools with your text editor.