Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go Programming Blueprints

You're reading from  Go Programming Blueprints

Product type Book
Published in Jan 2015
Publisher Packt
ISBN-13 9781783988020
Pages 274 pages
Edition 1st Edition
Languages
Author (1):
Mat Ryer Mat Ryer
Profile icon Mat Ryer
Toc

Table of Contents (17) Chapters close

Go Programming Blueprints
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. Chat Application with Web Sockets 2. Adding Authentication 3. Three Ways to Implement Profile Pictures 4. Command-line Tools to Find Domain Names 5. Building Distributed Systems and Working with Flexible Data 6. Exposing Data and Functionality through a RESTful Data Web Service API 7. Random Recommendations Web Service 8. Filesystem Backup Good Practices for a Stable Go Environment Index

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 see 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:

go fmt

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 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 afterwards. The go vet tool has noticed this and points out that we have unreachable code in our file.

Tip

If you get an error running any Go tools, it usually means you have to get the command before you can use it. However, in the case of the vet tool, you just have to open a terminal and run:

go get code.google.com/p/go.tools/cmd/vet

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 towards 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://godoc.org/code.google.com/p/go.tools/cmd/vet.

The final tool we will play with is called goimports, and 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 the familiar command:

go get code.google.com/p/go.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 are telling 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 notice 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}