Book Image

Mastering Go - Second Edition

By : Mihalis Tsoukalos
Book Image

Mastering Go - Second Edition

By: Mihalis Tsoukalos

Overview of this book

Often referred to (incorrectly) as Golang, Go is the high-performance systems language of the future. Mastering Go, Second Edition helps you become a productive expert Go programmer, building and improving on the groundbreaking first edition. Mastering Go, Second Edition shows how to put Go to work on real production systems. For programmers who already know the Go language basics, this book provides examples, patterns, and clear explanations to help you deeply understand Go’s capabilities and apply them in your programming work. The book covers the nuances of Go, with in-depth guides on types and structures, packages, concurrency, network programming, compiler design, optimization, and more. Each chapter ends with exercises and resources to fully embed your new knowledge. This second edition includes a completely new chapter on machine learning in Go, guiding you from the foundation statistics techniques through simple regression and clustering to classification, neural networks, and anomaly detection. Other chapters are expanded to cover using Go with Docker and Kubernetes, Git, WebAssembly, JSON, and more. If you take the Go programming language seriously, the second edition of this book is an essential guide on expert techniques.
Table of Contents (20 chapters)
Title Page

Downloading Go packages

Although the standard Go library is very rich, there are times that you will need to download external Go packages in order to use their functionality. This section will teach you how to download an external Go package and where it will be placed on your UNIX machine.

Have in mind that although Go modules, which is a new Go feature that is still under development, might introduce changes to the way you work with external Go code, the process of downloading a single Go package into your computer will remain the same.
You will learn a lot more about Go packages and Go modules in Chapter 6, What You Might Not Know About Go Packages and Go Functions.

Look at the following naive Go program that is saved as getPackage.go:

package main 
 
import ( 
    "fmt" 
    "github.com/mactsouk/go/simpleGitHub" 
) 
 
func main() { 
    fmt.Println(simpleGitHub.AddTwo(5, 6)) 
} 

This program uses an external package because one of the import commands uses an internet address. In this case, the external package is called simpleGitHub and is located at github.com/mactsouk/go/simpleGitHub.

If you try to execute getPackage.go right away, you will be disappointed:

$ go run getPackage.go
getPackage.go:5:2: cannot find package "github.com/mactsouk/go/simpleGitHub" in any of:
    /usr/local/Cellar/go/1.9.1/libexec/src/github.com/mactsouk/go/
simpleGitHub (from $GOROOT)
/Users/mtsouk/go/src/github.com/mactsouk/go/simpleGitHub (from $GOPATH)

So, you will need to get the missing package on your computer. In order to download it, you will need to execute the following command:

$ go get -v github.com/mactsouk/go/simpleGitHub
github.com/mactsouk/go (download)
github.com/mactsouk/go/simpleGitHub
  

After that, you can find the downloaded files at the following directory:

$ ls -l ~/go/src/github.com/mactsouk/go/simpleGitHub/
total 8
-rw-r--r--  1 mtsouk  staff  66 Oct 17 21:47 simpleGitHub.go
  

However, the go get command also compiles the package. The relevant files can be found at the following place:

$ ls -l ~/go/pkg/darwin_amd64/github.com/mactsouk/go/simpleGitHub.a
-rw-r--r--  1 mtsouk  staff  1050 Oct 17 21:47 /Users/mtsouk/go/pkg/darwin_amd64/github.com/mactsouk/go/simpleGitHub.a
  

You are now ready to execute getPackage.go without any problems:

$ go run getPackage.go
11
  

You can delete the intermediate files of a downloaded Go package as follows:

$ go clean -i -v -x github.com/mactsouk/go/simpleGitHub
cd /Users/mtsouk/go/src/github.com/mactsouk/go/simpleGitHub
rm -f simpleGitHub.test simpleGitHub.test.exe
rm -f /Users/mtsouk/go/pkg/darwin_amd64/github.com/mactsouk/go/
simpleGitHub.a

Similarly, you can delete an entire Go package you have downloaded locally using the rm(1) UNIX command to delete its Go source after using go clean:

$ go clean -i -v -x github.com/mactsouk/go/simpleGitHub
$ rm -rf ~/go/src/github.com/mactsouk/go/simpleGitHub
  

After executing the former commands, you will need to download the Go package again.