Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go: Design Patterns for Real-World Projects

You're reading from   Go: Design Patterns for Real-World Projects Build production-ready solutions in Go using cutting-edge technology and techniques

Arrow left icon
Product type Course
Published in Jun 2017
Publisher Packt
ISBN-13 9781788390552
Length 1091 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Mario Castro Contreras Mario Castro Contreras
Author Profile Icon Mario Castro Contreras
Mario Castro Contreras
Mat Ryer Mat Ryer
Author Profile Icon Mat Ryer
Mat Ryer
Vladimir Vivien Vladimir Vivien
Author Profile Icon Vladimir Vivien
Vladimir Vivien
Arrow right icon
View More author details
Toc

Table of Contents (38) Chapters Close

Go: Design Patterns for Real-World Projects
Credits
Preface
Bibliography
1. A First Step in Go FREE CHAPTER 2. Go Language Essentials 3. Go Control Flow 4. Data Types 5. Functions in Go 6. Go Packages and Programs 7. Composite Types 8. Methods, Interfaces, and Objects 9. Concurrency 10. Data IO in Go 11. Writing Networked Services 12. Code Testing 13. Ready... Steady... Go! 14. Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns 15. Structural Patterns - Composite, Adapter, and Bridge Design Patterns 16. Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns 17. Behavioral Patterns - Strategy, Chain of Responsibility, and Command Design Patterns 18. Behavioral Patterns - Template, Memento, and Interpreter Design Patterns 19. Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns 20. Introduction to Gos Concurrency 21. Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns 22. Concurrency Patterns - Workers Pool and Publish/Subscriber Design Patterns 23. Chat Application with Web Sockets 24. Adding User Accounts 25. Three Ways to Implement Profile Pictures 26. Command-Line Tools to Find Domain Names 27. Building Distributed Systems and Working with Flexible Data 28. Exposing Data and Functionality through a RESTful Data Web Service API 29. Random Recommendations Web Service 30. Filesystem Backup 31. Building a Q&A Application for Google App Engine 32. Micro-services in Go with the Go kit Framework 33. Deploying Go Applications Using Docker 1. Good Practices for a Stable Go Environment

Configuring Go


Go is now installed, but in order to use the tools, we must ensure that it is properly configured. To make calling the tools easier, we need to add our go/bin path to the PATH environment variable.

Note

On Unix systems, you should add export PATH=$PATH:/opt/go/bin (make sure it is the path you chose when installing Go) to your .bashrc file.

On Windows, open System Properties (try right-clicking on My Computer), and under Advanced, click on the Environment Variables button and use the UI to ensure that the PATH variable contains the path to your go/bin folder.

In a terminal (you may need to restart it for your changes to take effect), you can make sure this worked by printing the value of the PATH variable:

echo $PATH

Ensure that the value printed contains the correct path to your go/bin folder; for example, on my machine it prints as follows:

/usr/local/bin:/usr/bin:/bin:/opt/go/bin

Note

The colons (semicolons on Windows) between the paths indicate that the PATH variable is actually a list of folders rather than just one folder. This indicates that each folder included will be searched when you enter commands in your terminal.

Now we can make sure the Go build we just made runs successfully:

go version

Executing the go command (which can be found in your go/bin location) like this will print out the current version for us. For example, for Go 1.77.1, you should see something similar to the following:

go version go1.77.1 darwin/amd64

Getting GOPATH right

GOPATH is another environment variable to a folder (such as PATH in the previous section) that is used to specify the location for the Go source code and the compiled binary packages. Using the import command in your Go programs will cause the compiler to look in the GOPATH location to find the packages you are referring to. When using go get and other commands, projects are downloaded into the GOPATH folder.

While the GOPATH location can contain a list of colon-separated folders, such as PATH and you can even have a different value for GOPATH depending on which project you are working in it is strongly recommended that you use a single GOPATH location for everything, and this is what we will assume you will do for the projects in this book.

Create a new folder called go, this time in your Users folder somewhere perhaps in a Work subfolder. This will be our GOPATH target and is where all the third-party code and binaries will end up as well as where we will write our Go programs and packages. Using the same technique you used when setting the PATH environment variable in the previous section, set the GOPATH variable to the new go folder. Let's open a terminal and use one of the newly installed commands to get a third-party package for us to use:

go get github.com/matryer/silk

Getting the silk library will actually cause this folder structure to be created: $GOPATH/src/github.com/matryer/silk. You can see that the path segments are important in how Go organizes things, which helps namespace projects and keeps them unique. For example, if you created your own package called silk, you wouldn't keep it in the GitHub repository of matryer, so the path would be different.

When we create projects in this book, you should consider a sensible GOPATH root for them. For example, I used github.com/matryer/goblueprints, and if you were to go get that, you would actually get a complete copy of all the source code for this book in your GOPATH folder!

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 $19.99/month. Cancel anytime