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
Mastering Go

You're reading from   Mastering Go Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures

Arrow left icon
Product type Paperback
Published in Aug 2019
Publisher Packt
ISBN-13 9781838559335
Length 798 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mihalis Tsoukalos Mihalis Tsoukalos
Author Profile Icon Mihalis Tsoukalos
Mihalis Tsoukalos
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

1. Go and the Operating System 2. Understanding Go Internals FREE CHAPTER 3. Working with Basic Go Data Types 4. The Uses of Composite Types 5. How to Enhance Go Code with Data Structures 6. What You Might Not Know About Go Packages and Functions 7. Reflection and Interfaces for All Seasons 8. Telling a UNIX System What to Do 9. Concurrency in Go – Goroutines, Channels, and Pipelines 10. Concurrency in Go – Advanced Topics 11. Code Testing, Optimization, and Profiling 12. The Foundations of Network Programming in Go 13. Network Programming – Building Your Own Servers and Clients 14. Machine Learning in Go 15. Other Books You May Enjoy

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.

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 €18.99/month. Cancel anytime