Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Go Design Patterns
Go Design Patterns
Go Design Patterns
Ebook781 pages6 hours

Go Design Patterns

Rating: 5 out of 5 stars

5/5

()

Read preview

About this ebook

About This Book
  • A highly practical guide filled with numerous examples unleashing the power of design patterns with Go.
  • Discover an introduction of the CSP concurrency model by explaining GoRoutines and channels.
  • Get a full explanation, including comprehensive text and examples, of all known GoF design patterns in Go.
Who This Book Is For

The target audience is both beginner- and advanced-level developers in the Go programming language. No knowledge of design patterns is expected.

LanguageEnglish
Release dateFeb 24, 2017
ISBN9781786461902
Go Design Patterns

Related to Go Design Patterns

Related ebooks

Computers For You

View More

Related articles

Reviews for Go Design Patterns

Rating: 5 out of 5 stars
5/5

1 rating0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Go Design Patterns - Mario Castro Contreras

    Table of Contents

    Go Design Patterns

    Credits

    About the Author

    About the Reviewer

    www.PacktPub.com

    Why subscribe?

    Customer Feedback

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Errata

    Piracy

    Questions

    1. Ready... Steady... Go!

    A little bit of history

    Installing Go

    Linux

    Go Linux advanced installation

    Windows

    Mac OS X

    Setting the workspace - Linux and Apple OS X

    Starting with Hello World

    Integrated Development Environment - IDE

    Types

    Variables and constants

    Operators

    Flow control

    The if... else statement

    The switch statement

    The for…range statement

    Functions

    What does a function look like?

    What is an anonymous function?

    Closures

    Creating errors, handling errors and returning errors.

    Function with undetermined number of parameters

    Naming returned types

    Arrays, slices, and maps

    Arrays

    Zero-initialization

    Slices

    Maps

    Visibility

    Zero-initialization

    Pointers and structures

    What is a pointer? Why are they good?

    Structs

    Interfaces

    Interfaces - signing a contract

    Testing and TDD

    The testing package

    What is TDD?

    Libraries

    The Go get tool

    Managing JSON data

    The encoding package

    Go tools

    The golint tool

    The gofmt tool

    The godoc tool

    The goimport tool

    Contributing to Go open source projects in GitHub

    Summary

    2. Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns

    Singleton design pattern - having a unique instance of a type in the entire program

    Description

    Objectives

    Example - a unique counter

    Requirements and acceptance criteria

    Writing unit tests first

    Implementation

    A few words about the Singleton design pattern

    Builder design pattern - reusing an algorithm to create many implementations of an interface

    Description

    Objectives

    Example - vehicle manufacturing

    Requirements and acceptance criteria

    Unit test for the vehicle builder

    Implementation

    Wrapping up the Builder design pattern

    Factory method - delegating the creation of different types of payments

    Description

    Objectives

    The example - a factory of payment methods for a shop

    Acceptance criteria

    First unit test

    Implementation

    Upgrading the Debitcard method to a new platform

    What we learned about the Factory method

    Abstract Factory - a factory of factories

    Description

    The objectives

    The vehicle factory example, again?

    Acceptance criteria

    Unit test

    Implementation

    A few lines about the Abstract Factory method

    Prototype design pattern

    Description

    Objective

    Example

    Acceptance criteria

    Unit test

    Implementation

    What we learned about the Prototype design pattern

    Summary

    3. Structural Patterns - Composite, Adapter, and Bridge Design Patterns

    Composite design pattern

    Description

    Objectives

    The swimmer and the fish

    Requirements and acceptance criteria

    Creating compositions

    Binary Tree compositions

    Composite pattern versus inheritance

    Final words on the Composite pattern

    Adapter design pattern

    Description

    Objectives

    Using an incompatible interface with an Adapter object

    Requirements and acceptance criteria

    Unit testing our Printer adapter

    Implementation

    Examples of the Adapter pattern in Go's source code

    What the Go source code tells us about the Adapter pattern

    Bridge design pattern

    Description

    Objectives

    Two printers and two ways of printing for each

    Requirements and acceptance criteria

    Unit testing the Bridge pattern

    Implementation

    Reuse everything with the Bridge pattern

    Summary

    4. Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns

    Proxy design pattern

    Description

    Objectives

    Example

    Acceptance criteria

    Unit test

    Implementation

    Proxying around actions

    Decorator design pattern

    Description

    Objectives

    Example

    Acceptance criteria

    Unit test

    Implementation

    A real-life example - server middleware

    Starting with the common interface, http.Handler

    A few words about Go's structural typing

    Summarizing the Decorator design pattern - Proxy versus Decorator

    Facade design pattern

    Description

    Objectives

    Example

    Acceptance criteria

    Unit test

    Implementation

    Library created with the Facade pattern

    Flyweight design pattern

    Description

    Objectives

    Example

    Acceptance criteria

    Basic structs and tests

    Implementation

    What's the difference between Singleton and Flyweight then?

    Summary

    5. Behavioral Patterns - Strategy, Chain of Responsibility, and Command Design Patterns

    Strategy design pattern

    Description

    Objectives

    Rendering images or text

    Acceptance criteria

    Implementation

    Solving small issues in our library

    Final words on the Strategy pattern

    Chain of responsibility design pattern

    Description

    Objectives

    A multi-logger chain

    Unit test

    Implementation

    What about a closure?

    Putting it together

    Command design pattern

    Description

    Objectives

    A simple queue

    Acceptance criteria

    Implementation

    More examples

    Chain of responsibility of commands

    Rounding-up the Command pattern up

    Summary

    6. Behavioral Patterns - Template, Memento, and Interpreter Design Patterns

    Template design pattern

    Description

    Objectives

    Example - a simple algorithm with a deferred step

    Requirements and acceptance criteria

    Unit tests for the simple algorithm

    Implementing the Template pattern

    Anonymous functions

    How to avoid modifications on the interface

    Looking for the Template pattern in Go's source code

    Summarizing the Template design pattern

    Memento design pattern

    Description

    Objectives

    A simple example with strings

    Requirements and acceptance criteria

    Unit test

    Implementing the Memento pattern

    Another example using the Command and Facade patterns

    Last words on the Memento pattern

    Interpreter design pattern

    Description

    Objectives

    Example - a polish notation calculator

    Acceptance criteria for the calculator

    Unit test of some operations

    Implementation

    Complexity with the Interpreter design pattern

    Interpreter pattern again - now using interfaces

    The power of the Interpreter pattern

    Summary

    7. Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns

    Visitor design pattern

    Description

    Objectives

    A log appender

    Acceptance criteria

    Unit tests

    Implementation of Visitor pattern

    Another example

    Visitors to the rescue!

    State design pattern

    Description

    Objectives

    A small guess the number game

    Acceptance criteria

    Implementation of State pattern

    A state to win and a state to lose

    The game built using the State pattern

    Mediator design pattern

    Description

    Objectives

    A calculator

    Acceptance criteria

    Implementation

    Uncoupling two types with the Mediator

    Observer design pattern

    Description

    Objectives

    The notifier

    Acceptance criteria

    Unit tests

    Implementation

    Summary

    8. Introduction to Gos Concurrency

    A little bit of history and theory

    Concurrency versus parallelism

    CSP versus actor-based concurrency

    Goroutines

    Our first Goroutine

    Anonymous functions launched as new Goroutines

    WaitGroups

    Callbacks

    Callback hell

    Mutexes

    An example with mutexes - concurrent counter

    Presenting the race detector

    Channels

    Our first channel

    Buffered channels

    Directional channels

    The select statement

    Ranging over channels too!

    Using it all - concurrent singleton

    Unit test

    Implementation

    Summary

    9. Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns

    Barrier concurrency pattern

    Description

    Objectives

    An HTTP GET aggregator

    Acceptance criteria

    Unit test - integration

    Implementation

    Waiting for responses with the Barrier design pattern

    Future design pattern

    Description

    Objectives

    A simple asynchronous requester

    Acceptance criteria

    Unit tests

    Implementation

    Putting the Future together

    Pipeline design pattern

    Description

    Objectives

    A concurrent multi-operation

    Acceptance criteria

    Beginning with tests

    Implementation

    The list generator

    Raising numbers to the power of 2

    Final reduce operation

    Launching the Pipeline pattern

    Final words on the Pipeline pattern

    Summary

    10. Concurrency Patterns - Workers Pool and Publish/Subscriber Design Patterns

    Workers pool

    Description

    Objectives

    A pool of pipelines

    Acceptance criteria

    Implementation

    The dispatcher

    The pipeline

    An app using the workers pool

    No tests?

    Wrapping up the Worker pool

    Concurrent Publish/Subscriber design pattern

    Description

    Objectives

    Example - a concurrent notifier

    Acceptance criteria

    Unit test

    Testing subscriber

    Testing publisher

    Implementation

    Implementing the publisher

    Handling channels without race conditions

    A few words on the concurrent Observer pattern

    Summary

    Go Design Patterns


    Go Design Patterns

    Copyright © 2017 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    First published: February 2017

    Production reference: 1170217

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham 

    B3 2PB, UK.

    ISBN  978-1-78646-620-4

    www.packtpub.com

    Credits

    About the Author

    Mario Castro Contreras is a software engineer who has specialized in distributed systems and big data solutions. He works as a site reliability engineer, and now he is focused on containerized solutions and apps using most of Google Cloud suite; especially, Kubernetes. He has a wide experience in systems and solutions integration, and he has written many scalable and reliable 12 factor apps using Go and Docker. He has designed Big Data  architectures for financial services and media, and he has written data processing pipelines using event-driven architectures written purely in Go. He is also very active in the open source community, and you can find him on his GitHub account with the username sayden.  In the past, he has also written mobile applications and backends in Java.

    Mario is passionate about programming languages, and he found the best balance between fun and productivity in Go; however, recently, he enjoys writing in Rust and embedded systems in C. He is also passionate about road cycling and winter sports.

    I'd like to express my deep gratitude to my parents for supporting me in my journey through computers since I was 8. To Urszula, Tyrion and Tesla for their daily support and for being with me in the long nights writing this book.

    I'd like to thank Chaitanya, for her guidance at the beginning of the book, Zeeyan, for his patience and help on every chapter, and Pavan, for the help and explanations. But also to all the reviewers, especially to Shiju, and the entire team at Packt that made this book possible.

    About the Reviewer

    Shiju Varghese is a solutions architect focused on building highly scalable cloud native applications with a special interest in APIs, microservices, containerized architectures, and distributed systems. He currently specializes in Go, Google Cloud, and container technologies. He is an early adopter of the Go programming language and provides consultation and training for building backend systems and microservices with Go ecosystem. He has been a mentor to various start-ups and enterprises for the technology transformation to Go. He has been a speaker at numerous technology conferences, including GopherCon India.

    Shiju has authored two books on Go, titled Web Development with Go and Go Recipes, both published by Apress.

    www.PacktPub.com

    For support files and downloads related to your book, please visit www.PacktPub.com.

    Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.comfor more details.

    At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

    https://www.packtpub.com/mapt

    Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

    Why subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print, and bookmark content

    On demand and accessible via a web browser

    Customer Feedback

    Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1786466201.

    If you'd like to join our team of regular reviewers, you can email us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

    Preface

    Welcome to the book Go Design Patterns! With this book, you'll learn basic and advanced techniques and patterns with the Go language. Don't worry if you have never written Go code before; this book will gradually introduce you to the various concepts in Go programming. At the same time, experts will find many tips and tricks on the language, so I encourage you to not miss any chapter. If you already know the classic design patterns, you'll find this book very handy, not only as a reference book but also as a way to learn idiomatic Go approaches to solve common problems that you may already know.

    The book is divided in three sections:

    Introduction to the Go language: This is the first part of the book, where you'll learn the basic syntax, the tools that comes with the binary distributions, basic testing, JSON parsing, and more. We leave concurrency for a later chapter to focus on the way that the syntax and the compiler work in a typical Go app.

    Classic design patterns in idiomatic Go: The second section presents the classic design patterns but as we will see, they are quite different, partly because of the lack of inheritance in Go, but also because we have different and more optimal ways to solve the same problems. A newcomer to the language will find the examples in this section very useful as a way to understand the roots of Go and the idiomatic ways in which you can solve problems using Go in the same manner as you would solve in languages such as Java or C++. Most examples are presented by using TDD and some of them even show examples within Go standard library that uses these patterns.

    Concurrency patterns: The focus in this section is learning about concurrent structures and parallel execution. You will learn most of the primitives in Go to write concurrent apps, and we will develop some of the classical design patterns with concurrent structures to maximize parallelism. Also, we will learn some of the typical structures to develop concurrent apps in Go. You learn how a classical pattern can become more complex if we need it to work in a concurrent way but the idea is to understand Go concurrent primitives so that the reader finishes the book knowing how to write their own concurrent design patterns by using the knowledge taken from the book.

    The book will slowly raise the difficulty of some tasks. We have explained tips and tricks in every chapter.

    What this book covers

    Chapter 1 , Ready… Steady…Go!, attempts to help newcomers to the Go programming language who have some background in any other programming language. It will begin by showing how to install the Go environment in a Linux machine, moving to syntax, type and flow control.

    Chapter 2 , Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns, introduces the problems that can arise when an object creation or management is particularly complex or expensive using the Singleton, Builder, Factory, and Abstract Factory design patterns.

    Chapter 3 , Structural Patterns - Composite, Adapter, and Bridge Design Patterns, deals with the first set of Structural patterns about object composition to get some new functionality. Such as creating an intermediate object and using of various objects as if there is only one.

    Chapter 4 , Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns, is less oriented to multi-object composition but focuses more on obtaining new functionality in existing objects. The Decorator pattern is commonly used to follow the open-closed principle. Facade is extensively used in API’s where you want a single source for many sources of information and actions. Flyweight is not so common but it’s a very useful pattern when the memory is becoming a problem caused by a large collection of similar objects. Finally, the Proxy pattern wraps on an object to provide the same functionality, but at the same time, adding something to the proxy’s functionality.

    Chapter 5 ,  Behavioral patterns - Strategy, Chain of Responsibility, Command, and Mediator Design Patterns, deals with the first behavioral pattern to make objects react in an expected or bounded way. We’ll start with the Strategy pattern, perhaps the most important design pattern in object-oriented programming, as many design patterns have something in common with it. Then we’ll move to the Chain of Responsibility to build chains of objects that can decide which between them must deal with a particular case. Finally, Command pattern to encapsulate actions that don’t necessarily need to be executed immediately or must be stored.

    Chapter 6 , Behavioral Patterns - Template, Memento, and Interpreter Design Patterns, continues with Behavioral patterns introducing the Interpreter pattern, a quite complex pattern to create small languages and Interpreters for them. It can be very useful when a problem can be solved by inventing a small language for it. The Memento pattern is in front of our eyes every day with the Undo button in apps. The Template pattern helps developers by defining an initial structure of an operation so that the final users of the code can finish it.

    Chapter 7 , Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns, depicts the Observer pattern, an important pattern that is becoming tremendously popular in distributed systems and reactive programming. The Visitor pattern deals with complex hierarchies of objects where you need to apply a particular action depending on the object. Finally, the State pattern is commonly used in video games and finite state machines and allows an object to change its behavior depending on its own state.

    Chapter 8 , Introduction to Go's Concurrency, explains with more detail the CSP concurrency model used in Go by going through some examples using Goroutines and channels, as well as mutexes and syncs.

    Chapter 9 , Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns, will introduce some of the CSP concurrency patterns that are idiomatic to the Go language by walking through some examples and explanations. These are small but really powerful patterns so we will provides a few examples of the use of each of them, as well as some schemas (if possible) that will make the understanding of each of them easier.

    Chapter 10 ,  Concurrency Patterns -  Workers Pool, and Publish or Subscriber Design Patterns, talks about a couple of patterns with concurrent structures. We will explain every step in detail so you can follow the examples carefully. The idea is to learn patterns to design concurrent applications in idiomatic Go. We are using channels and Goroutines heavily, instead of locks or sharing variables.

    What you need for this book

    Most of the chapters in this book are written following a simple TDD approach, here the requirements are written first, followed by some unit tests and finally the code that satisfies those requirements. We will use only tools that comes with the standard library of Go as a way to better understand the language and its possibilities. This idea is key to follow the book and understanding the way that Go solves problems, especially in distributed systems and concurrent applications.

    Who this book is for

    This book is for both beginners and advanced-level developers in the Go programming language. No knowledge of design patterns is expected.

    Conventions

    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: we need a main function to use it as libraries cannot be converted to executable files directly.

    A block of code is set as follows:

        package main

     

        func main() {

          ten := 10

          if ten == 20 {

            println(This shouldn't be printed as 10 isn't equal to 20)

          } else {

            println(Ten is not equals to 20)

          }

        }

    When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

        if a == b || 10 == 10 || true == false {

     

          println(10 is equal to 10)

        }

    else if 11 == 11 && go == go

    {

            println(This won't because previous condition was satisfied)

          }

        }

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

    $ go run main.go

    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: In order to download new modules, we will go to Files | Settings | Project Name | Project Interpreter.

    Note

    Warnings or important notes appear in a box like this.

    Tip

    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 feedback@packtpub.com, 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 www.packtpub.com/authors.

    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 http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

    You can download the code files by following these steps:

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

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

    Click on Code Downloads & Errata.

    Enter the name of the book in the Search box.

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

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

    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 https://github.com/PacktPublishing/Go-Design-Patterns. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

    Errata

    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 http://www.packtpub.com/submit-errata, 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 https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

    Piracy

    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 copyright@packtpub.com with a link to the suspected pirated material.

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

    Questions

    If you have a problem with any aspect of this book, you can contact us at questions@packtpub.com, and we will do our best to address the problem.

    Chapter 1. Ready... Steady... Go!

    Design Patterns have been the foundation for hundreds of thousands of pieces of software. Since the Gang Of Four (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) wrote the book Design Patterns: Elements of Reusable Object-Oriented Software in 1994 with examples in C++ and Smalltalk, the twenty-three classic patterns have been re-implemented in most of major languages of today and they have been used in almost every project you know about.

    The Gang of Four detected that many small architectures were present in many of their projects, they started to rewrite them in a more abstract way and they released the famous book.

    This book is a comprehensive explanation and implementation of the most common design patterns from the Gang of Four and today's patterns plus some of the most idiomatic concurrency patterns in Go.

    But what is Go...?

    A little bit of history

    On the last 20 years, we have lived an incredible growth in computer science. Storage spaces have been increased dramatically, RAM has suffered a substantial growth, and CPU's are... well... simply faster. Have they grown as much as storage and RAM memory? Not really, CPU industry has reached a limit in the speed that their CPU's can deliver, mainly because they have become so fast that they cannot get enough power to work while they dissipate enough heat. The CPU manufacturers are now shipping more cores on each computer. This situation crashes against the background of many systems programming languages that weren't designed for multi-processor CPUs or large distributed systems that act as a unique machine. In Google, they realized that this was becoming more than an issue while they were struggling to develop distributed applications in languages like Java or C++ that weren't designed with concurrency in mind.

    At the same time, our programs were bigger, more complex, more difficult to maintain and with a lot of room for bad practices. While our computers had more cores and were faster, we were not faster when developing our code neither our distributed applications. This was Go's target.

    Go design started in 2007 by three Googlers in the research of a programming language that could solve common issues in large scale distributed systems like the ones you can find at Google. The creators were:

    Rob Pike: Plan 9 and Inferno OS.

    Robert Griesemer: Worked at Google's V8 JavaScript engine that powers Google Chrome.

    Ken Thompson: Worked at Bell labs and the Unix team. It has been involved in designing of the Plan 9 operating system as well as the definition of the UTF-8 encoding.

    In 2008, the compiler was done and the team got the help of Russ Cox and Ian Lance Taylor. The team started their journey to open source the project in 2009 and in March 2012 they reached a version 1.0 after more than fifty releases.

    Installing Go

    Any Go Installation needs two basic things: the binaries of the language somewhere on your disk and a GOPATH path in your system where your projects and the projects that you download from other people will be stored.

    In the following lines, we will explore how to install Go binaries in Linux, Windows and OS X. For a detailed explanation of how to install the latest version of Go, you can refer to the official documentation at https://golang.org/doc/install.

    Linux

    To install Go in Linux you have two options:

    Easy option: Use your distribution package manager:

    RHEL/Fedora/Centos users with YUM/DNF:sudo yum install -y golang

    Ubuntu/Debian users using APT with:sudo apt-get install -y golang

    Advanced: Downloading the latest distribution from https://golang.org.

    I recommend using the second and downloading a distribution. Go's updates maintains backward compatibility and you usually should not be worried about updating your Go binaries frequently.

    Go Linux advanced installation

    The advanced installation of Go in Linux requires you to download the binaries from golang webpage. After entering https://golang.org , click the Download Go button (usually at the right) some Featured Downloads option is available for each distribution. Select Linux distribution to download the latest stable version.

    Note

    At https://golang.org you can also download beta versions of the language.

    Let's say we have saved the tar.gz file in Downloads folder so let's extract it and move it to a different path. By convention, Go binaries are usually placed in /usr/local/go directory:

    tar -zxvf go*.*.*.linux-amd64.tar.gz sudo mv go /usr/local/go

    On extraction remember to replace asterisks (*) with the version you have downloaded.

    Now we have our Go installation in/usr/local/go path so now we have to add the bin subfolder to our PATH and the bin folder within our GOPATH.

    mkdir -p $HOME/go/bin

    With -p we are telling bash to create all directories that are necessary. Now we need to append bin folder paths to our PATH, append the following lines at the end of your ~/.bashrc:

    export PATH=$PATH:/usr/local/go/bin

    Check that our go/bin directory is available:

    $ go version Go version go1.6.2 linux/amd64

    Windows

    To install Go in Windows, you will need administrator privileges. Open your favorite browser and navigate to https://golang.org . Once there click the Download Go button and select Microsoft Windows distribution. A *.msi file will start downloading.

    Execute the MSI installer by double clicking it. An installer will appear asking you to accept the End User License Agreement (EULA) and select a target folder for your installation. We will continue with the default path that in my case was C:\Go.

    Once the installation is finished you will have to add the binary Go folder, located in C:\Go\bin to your Path. For this, you must go to Control Panel and select System option. Once in System, select the Advanced tab and click the Environment variables button. Here you'll find a window with variables for your current user and system variables. In system variables, you'll find the Path variable. Click it and click the Edit  button to open a text box. You can add your path by adding ;C:\Go/bin at the end of the current line (note the semicolon at the beginning of

    Enjoying the preview?
    Page 1 of 1