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

Integrated developer environments


The Integrated Developer Environments (IDEs)  are essentially text editors with additional features that make writing code and building software easier. Text with special meaning, such as string literals, types, function names, and so on are often colored differently by syntax highlighting, or you may get autocomplete options as you're typing. Some editors even point out errors in your code before you've executed it.

There are many options to choose from, and mostly, it comes down to personal preference, but we will look at some of the more popular choices as well as how to set them up to build Go projects.

The most popular editors include the following:

  • Sublime Text 3

  • Visual Studio Code

  • Atom

  • Vim (with vim-go)

You can see a complete curated list of options at https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins.

In this section, we are going to explore Sublime Text 3 and Visual Studio Code.

Sublime Text 3

Sublime Text 3 is an excellent editor to write Go code that runs on OS X, Linux, and Windows and has an extremely powerful expansion model, which makes it easy to customize and extend. You can download Sublime Text from http://www.sublimetext.com/ and trial-use it for free before deciding whether you want to buy it or not.

Thanks to DisposaBoy (refer to https://github.com/DisposaBoy), there is already a Sublime expansion package for Go, which actually gives us a wealth of features and power that a lot of Go programmers actually miss out on. We are going to install this GoSublime package and then build upon it to add our desired on-save functionality.

Before we can install GoSublime, we need to install Package Control into Sublime Text. Head over to https://sublime.wbond.net/ and click on the Installation link for instructions on how to install Package Control. At the time of writing this, it's simply a case of copying the single, albeit long, line command and pasting it into the Sublime console, which can be opened by navigating to View | Show Console from the menu.

Once this is complete, press shift + command + P and type Package Control: Install Package and press return when you have selected the option. After a short delay (where Package Control is updating its listings), a box will appear, allowing you to search for and install GoSublime just by typing it in, selecting it, and pressing return. If all is well, GoSublime will be installed and writing Go code will just become an order of magnitude easier.

Tip

Now that you have GoSublime installed, you can open a short help file containing the details of the package by pressing command + ., command + 2 (the command key and period at the same time, followed by the command key and number 2).

For some additional help while saving, press command + ., command + 5 to open the GoSublime settings and add the following entry to the object:

"on_save": [ 
  { 
    "cmd": "gs9o_open",  
    "args": { 
      "run": ["sh", "go build . errors && go test -i && go test && 
       go vet && golint"], 
      "focus_view": false 
    } 
  } 
] 

Tip

Note that the settings file is actually a JSON object, so ensure that you add the on_save property without corrupting the file. For example, if you have properties before and after, ensure the appropriate commas are in place.

The preceding setting will tell Sublime Text to build the code looking for errors, install test dependencies, run tests, and vet the code whenever we save the file. Save the settings file (don't close it just yet), and let's see this in action.

Navigate to Choose File | Open... from the menu and select a folder to open for now, let's open our tooling folder. The simple user interface of Sublime Text makes it clear that we only have one file in our project right now: main.go. Click on the file and add some extra linefeeds, and add and remove some indenting. Then, navigate to File | Save from the menu, or press command + S. Note that the code is immediately cleaned up, and provided that you haven't removed the oddly placed return statement from main.go, you will notice that the console has appeared and is reporting the issue thanks to go vet:

main.go:8: unreachable code

Holding down command + shift and double-clicking on the unreachable code line in the console will open the file and jump the cursor to the right line in question. You can see how helpful this feature is going to be as you continue to write Go code.

If you add an unwanted import to the file, you will notice that on using on_save, you are told about the problem, but it wasn't automatically fixed. This is because we have another tweak to make. In the same settings file as the one you added the on_save property to, add the following property:

"fmt_cmd": ["goimports"]

This tells GoSublime to use the goimports command instead of go fmt. Save this file again, and head back to main.go. Add net/http to the imports again, remove fmt import, and save the file. Note that the unused package was removed, and fmt was put back again.

Visual Studio Code

A surprise entry in the running for best Go IDE is Microsoft's Visual Studio Code, available for free at https://code.visualstudio.com.

Once you've downloaded it from the website, open a Go file (any file with a .go extension) and note that Visual Studio Code asks whether you'd like to install the recommended plugins to make working with Go files easier:

Click on Show Recommendations and click on Install next to the suggested Go plugin:

It may ask you to restart Visual Studio Code to enable the plugin, and it may also ask you to install some additional commands:

Click on Install All to install all the dependencies, being sure to wait for the previous installation process to finish before initiating others. After a short while, you will notice that a few tools were installed.

Write some messy code (or copy and paste some from https://github.com/matryer/goblueprints/blob/master/appendixA/messycode/main.go) into Visual Studio Code and hit save. You will notice that the imports were fixed and the code was nicely formatted as per the Go standard.

There are many more features that you can make use of, but we won't dig into them further here.

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