Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Mastering Go
Mastering Go

Mastering Go: Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures , Second Edition

eBook
€27.98 €39.99
Paperback
€49.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mastering Go

Garbage collection

Garbage collection is the process of freeing up memory space that is not being used. In other words, the garbage collector sees which objects are out of scope and cannot be referenced anymore, and frees the memory space they consume. This process happens in a concurrent way while a Go program is running and not before or after the execution of the program. The documentation of the Go garbage collector implementation states the following:

"The GC runs concurrently with mutator threads, is type accurate (also known as precise), allows multiple GC threads to run in parallel. It is a concurrent mark and sweep that uses a write barrier. It is non-generational and non-compacting. Allocation is done using size segregated per P allocation areas to minimize fragmentation while eliminating locks in the common case."

There is lots of terminology here that will...

Calling C code from Go

Although Go intends to make your programming experience better and save you from the quirks of C, C remains a very capable programming language that is still useful. This means that there are situations, such as when using a database or a device driver written in C, that still require the use of C, which means that you will need to work with C code in your Go projects.

If you find yourself using this capability many times in the same project, you might need to reconsider your approach or your choice of programming language.

Calling C code from Go using the same file

The simplest way to call C code from a Go program is to include the C code in your Go source file. This needs special treatment but it is pretty...

Calling Go functions from C code

It is also possible to call a Go function from your C code. Therefore, this section will present you with a small example where two Go functions are going to be called from a C program. The Go package is going to be converted into a C shared library that is going to be used in the C program.

The Go package

This subsection will present you with the code of the Go package that will be used in a C program. The name of the Go package needs to be main but its filename can be anything you want; in this case, the filename will be usedByC.go and it will be presented in three parts.

You will learn more about Go packages in Chapter 6, What You Might Not Know About Go Packages and Go Functions.

The first part...

The defer keyword

The defer keyword postpones the execution of a function until the surrounding function returns, which is widely used in file input and output operations because it saves you from having to remember when to close an opened file. The defer keyword allows you to put the function call that closes an opened file near to the function call that opened it. As you will learn about the use of defer in file-related operations in Chapter 8, Telling a UNIX System What to Do, this section will present two different usages of defer. You will also see defer in action in the section that talks about the panic() and recover() built-in Go functions, as well as in the section that is related to logging.

It is very important to remember that deferred functions are executed in last in, first out (LIFO) order after the return of the surrounding function. Putting it simply, this means...

Panic and recover

This section will present you with a tricky technique that was first mentioned in the previous chapter. This technique involves the use of the panic() and recover() functions and will be presented in panicRecover.go, which you will see in three parts.

Strictly speaking, panic() is a built-in Go function that terminates the current flow of a Go program and starts panicking. On the other hand, the recover() function, which is also a built-in Go function, allows you to take back control of a goroutine that just panicked using panic().

The first part of the program is next:

package main 
 
import ( 
    "fmt" 
) 
 
func a() { 
    fmt.Println("Inside a()") 
    defer func() { 
        if c := recover(); c != nil { 
            fmt.Println("Recover inside a()!") 
        } 
    }() 
    fmt.Println("About to call b()") 
    b...

Two handy UNIX utilities

There are times when a UNIX program fails for some unknown reason or does not perform well and you want to find out why without having to rewrite your code and add a plethora of debugging statements.

This section will present two command-line utilities that allow you to see the C system calls executed by an executable file. The names of the two tools are strace(1) and dtrace(1) and they allow you to inspect the operation of a program.

Please remember that at the end of the day, all programs that work on UNIX machines end up using C system calls to communicate with the UNIX kernel and perform most of their tasks.

Although both tools can work with the go run command, you will get less unrelated output if you first create an executable file using go build and use this file. This mainly occurs because, as you already know, go run makes various temporary files...

Your Go environment

This section will talk about finding out information about your current Go environment using the functions and the properties of the runtime package. The name of the program that will be developed in this section is goEnv.go and it will be presented in two parts.

The first part of goEnv.go is next:

package main 
 
import ( 
    "fmt" 
    "runtime" 
) 

As you will see in a while, the runtime package contains functions and properties that will reveal the desired information. The second code portion of goEnv.go contains the implementation of the main() function:

func main() { 
    fmt.Print("You are using ", runtime.Compiler, " ") 
    fmt.Println("on a", runtime.GOARCH, "machine") 
    fmt.Println("Using Go version", runtime.Version()) 
    fmt.Println("Number of CPUs:", runtime.NumCPU...

The go env command

If you need to get a list of all environment variables supported by Go and the Go compiler, along with their current values, then the solution is to execute go env.

On my macOS Mojave, which uses Go version 1.11.4, the pretty rich output of go env is as follows:

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mtsouk/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mtsouk/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.4/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2&quot...

The Go assembler

This section will briefly talk about the assembly language and the Go assembler, which is a Go tool that allows you to see the assembly language used by the Go compiler.

As an example, you can see the assembly language of the goEnv.go program you saw in the previous section of this chapter by executing the next command:

$ GOOS=darwin GOARCH=amd64 go tool compile -S goEnv.go
  

The value of the GOOS variable defines the name of the target operating system whereas the value of the GOARCH variable defines the compilation architecture. The preceding command was executed on a macOS Mojave machine, hence the use of the darwin value for the GOOS variable.

The output of the previous command is pretty large even for a small program such as goEnv.go. Some of its output is next:

"".main STEXT size=859 args=0x0 locals=0x118
   0x0000 00000 (goEnv.go:8)       TEXT...

Node trees

A Go node is a struct with a large number of properties. You will learn more about defining and using Go structures in Chapter 4, The Uses of Composite Types. Everything in a Go program is parsed and analyzed by the modules of the Go compiler according to the grammar of the Go programming language. The final product of this analysis is a tree that is specific to the provided Go code and represents the program in a different way that is suited to the compiler rather than to the developer.

Please note that go tool 6g -W test.go does not work on newer Go versions. You should use go tool compile -W test.go instead.

This section will first use the following Go code, which is saved as nodeTree.go, as an example in order to see the kind of low-level information the go tool can provide us with:

package main 
 
import ( 
    "fmt" 
) 
 
func main() { 
    fmt.Println...

Finding out more about go build

If you want to learn more about what is happening behind the scenes when you execute a go build command, you should add the -x flag to it:

$ go build -x defer.go
WORK=/var/folders/sk/ltk8cnw50lzdtr2hxcj5sv2m0000gn/T/go-build254573394
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=/Users/mtsouk/Library/Caches/go-build/9d/9d6ca8651e083f3662adf82bb90a00837fc76f55839e65c7107bb55fcab92458-d
packagefile fmt=/usr/local/Cellar/go/1.11.4/libexec/pkg/darwin_amd64/fmt.a
packagefile runtime=/usr/local/Cellar/go/1.11.4/libexec/pkg/darwin_amd64/runtime.a
packagefile errors=/usr/local/Cellar/go/1.11.4/libexec/pkg/darwin_amd64/errors.a
packagefile io=/usr/local/Cellar/go/1.11.4/libexec/pkg/darwin_amd64/io.a
packagefile math=/usr/local/Cellar/go/1.11.4/libexec/pkg/darwin_amd64/math.a
packagefile...

Creating WebAssembly code

Go allows you to create WebAssembly code with the help of the go tool. Before I illustrate the process, I will share more information about WebAssembly.

A quick introduction to WebAssembly

WebAssembly (Wasm) is a machine model and executable format targeting a virtual machine. It is designed for efficiency, both in speed and file size. This means that you can use a WebAssembly binary on any platform you want without a single change.

WebAssembly comes in two formats: plain text format and binary format. Plain text format WebAssembly files have the .wat extension, whereas binary files have the .wasm file extension. Notice that once you have a WebAssembly binary file, you will have to load and use it using the...

General Go coding advice

The following is a list of practical advice that will help you to write better Go code:

  • If you have an error in a Go function, either log it or return it; do not do both unless you have a really good reason for doing so.
  • Go interfaces define behaviors not data and data structures.
  • Use the io.Reader and io.Writer interfaces when possible because they make your code more extensible.
  • Make sure that you pass a pointer to a variable to a function only when needed. The rest of the time, just pass the value of the variable.
  • Error variables are not string variables; they are error variables!
  • Do not test your Go code on production machines unless you have a really good reason to do so.
  • If you do not really know a Go feature, test it before using it for the first time, especially if you are developing an application or a utility that will be used by a large number...

Exercises and links

  • Learn more about the unsafe standard Go package by visiting its documentation page at https://golang.org/pkg/unsafe/.
  • Visit the web site of DTrace at http://dtrace.org/.
  • Use strace(1) on your Linux machine to inspect the operation of some standard UNIX utilities such as cp(1) and ls(1). What do you see?
  • If you are using a macOS machine, use dtruss(1) to see how the sync(8) utility works.
  • Write your own example where you use your own C code from a Go program.
  • Write a Go function and use it in a C program.
  • You can find more information about the functions of the runtime package by visiting https://golang.org/pkg/runtime/.
  • Reading research papers might be difficult but it is very rewarding. Please download the On-the-Fly Garbage Collection: An Exercise in Cooperation paper and read it. The paper can be found in many places, including https://dl.acm.org/citation...

Summary

This chapter discussed many interesting Go topics, including theoretical and practical information about the Go garbage collector; how to call C code from your Go programs; the handy and sometimes tricky defer keyword; the panic() and recover() functions; the strace(1), dtrace(1), and dtruss(1) UNIX tools; the use of the unsafe standard Go package; how to generate WebAssembly code from Go; and assembly code generated by Go. Finally, it shared information about your Go environment using the runtime package and showed how to reveal and explain the node tree of a Go program, before giving you some handy Go coding advice.

What you should remember from this chapter is that tools such as the unsafe Go package and the ability to call C code from Go are usually used on three occasions: firstly, when you want the best performance and you want to sacrifice some Go safety for it;...

Exercises and links

  • Learn more about the unsafe standard Go package by visiting its documentation page at https://golang.org/pkg/unsafe/.
  • Visit the web site of DTrace at http://dtrace.org/.
  • Use strace(1) on your Linux machine to inspect the operation of some standard UNIX utilities such as cp(1) and ls(1). What do you see?
  • If you are using a macOS machine, use dtruss(1) to see how the sync(8) utility works.
  • Write your own example where you use your own C code from a Go program.
  • Write a Go function and use it in a C program.
  • You can find more information about the functions of the runtime package by visiting https://golang.org/pkg/runtime/.
  • Reading research papers might be difficult but it is very rewarding. Please download the On-the-Fly Garbage Collection: An Exercise in Cooperation paper and read it. The paper can be found in many places, including https://dl.acm.org/citation...

Summary

This chapter discussed many interesting Go topics, including theoretical and practical information about the Go garbage collector; how to call C code from your Go programs; the handy and sometimes tricky defer keyword; the panic() and recover() functions; the strace(1), dtrace(1), and dtruss(1) UNIX tools; the use of the unsafe standard Go package; how to generate WebAssembly code from Go; and assembly code generated by Go. Finally, it shared information about your Go environment using the runtime package and showed how to reveal and explain the node tree of a Go program, before giving you some handy Go coding advice.

What you should remember from this chapter is that tools such as the unsafe Go package and the ability to call C code from Go are usually used on three occasions: firstly, when you want the best performance and you want to sacrifice some Go safety for it...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Second edition of the bestselling guide to advanced Go programming, expanded to cover machine learning, more Go packages and a range of modern development techniques
  • Completes the Go developer’s education with real-world guides to building high-performance production systems
  • Packed with practical examples and patterns to apply to your own development work
  • Clearly explains Go nuances and features to remove the frustration from Go development

Description

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.

Who is this book for?

Mastering Go, Second Edition is for Go programmers who already know the language basics, and want to become expert Go practitioners.

What you will learn

  • Clear guidance on using Go for production systems
  • Detailed explanations of how Go internals work, the design choices behind the language, and how to optimize your Go code
  • A full guide to all Go data types, composite types, and data structures
  • Master packages, reflection, and interfaces for effective Go programming
  • Build high-performance systems networking code, including server and client-side applications
  • Interface with other systems using WebAssembly, JSON, and gRPC
  • Write reliable, high-performance concurrent code
  • Build machine learning systems in Go, from simple statistical regression to complex neural networks

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 29, 2019
Length: 798 pages
Edition : 2nd
Language : English
ISBN-13 : 9781838555320
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Aug 29, 2019
Length: 798 pages
Edition : 2nd
Language : English
ISBN-13 : 9781838555320
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 115.97
Mastering Go
€49.99
The Go Workshop
€32.99
Go Programming Cookbook
€32.99
Total 115.97 Stars icon
Banner background image

Table of Contents

15 Chapters
Go and the Operating System Chevron down icon Chevron up icon
Understanding Go Internals Chevron down icon Chevron up icon
Working with Basic Go Data Types Chevron down icon Chevron up icon
The Uses of Composite Types Chevron down icon Chevron up icon
How to Enhance Go Code with Data Structures Chevron down icon Chevron up icon
What You Might Not Know About Go Packages and Functions Chevron down icon Chevron up icon
Reflection and Interfaces for All Seasons Chevron down icon Chevron up icon
Telling a UNIX System What to Do Chevron down icon Chevron up icon
Concurrency in Go – Goroutines, Channels, and Pipelines Chevron down icon Chevron up icon
Concurrency in Go – Advanced Topics Chevron down icon Chevron up icon
Code Testing, Optimization, and Profiling Chevron down icon Chevron up icon
The Foundations of Network Programming in Go Chevron down icon Chevron up icon
Network Programming – Building Your Own Servers and Clients Chevron down icon Chevron up icon
Machine Learning in Go Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1
(33 Ratings)
5 star 60.6%
4 star 12.1%
3 star 9.1%
2 star 15.2%
1 star 3%
Filter icon Filter
Top Reviews

Filter reviews by




cd Dec 22, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have just started reading Mastering Go, 2nd edition. The book contains many useful examples that can help someone learn Go and write Go code as well. What I find really handy is that it contains the required theory to understand the why of topics such Go data types, data structures, the Go Garbage Collector and the Go Scheduler.Overall, a great book.
Amazon Verified review Amazon
Vlad Bezden Nov 17, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book! It covers everything from how to start with Go to Machine Learning (ML) in Go. It has very good examples, with code and code output, so you don't have to write your code to see the result. Another good thing about this book that it covers Go version 1.13 and it has a chapter dedicated to Go modules and Go packages, which is very important. I read other books and usually, they don't cover this topic. Mihalis, cover this topic by going deeply starting with one package and then how to extend it to the module and how to pack, and deploy them. This book has two chapters on concurrency (Goroutines, channels, and pipelines), one chapter is to understand concurrency in Go and the second one is more advanced. At the end of each chapter, there is an "Exercises" chapter. Implementing those exercises you will learn much more about Go.I'm very strongly agreed that after you read this book you will become "master" in Go.
Amazon Verified review Amazon
No name Sep 19, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Based on the 1st edition but with more content. Still an excellent book that you can use as a reference and for learning Go.
Amazon Verified review Amazon
Kevin C Sep 20, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is huge and covers a lot of topics! This book is primarily for developers who want to gain a greater understanding of the power of Go and how to do more cool things with the language.Pros:- Concepts are explained in a simple manner. For example, the author introduces a new concept or tool with Go and explains why you might want to use it and when.- Made me a better Go developer. This book has a lot of 1% tips that improve your Go knowledge.- Diverse range of topics that explained in depth.- Easy to follow along code samplesCons:- This is a huge book! So don't expect to read it in a week or two. However, read it more like a reference book for when you interested in learning how to use Go to build different projects.- The code examples are not formatted when you copy and paste into your IDE. However, this is a common issue with programming books. You can quickly fix this with the "go fmt" command.Overall: I really love this book and would recommend it! I expect to go back to this book as I code Go throughout this next year as a guided reference.Side note: I particularly enjoyed the UNIX and Concurrency chapters because I'm interested in building CLI tools and the power of Go as your backend programming language.
Amazon Verified review Amazon
Spiros Sep 17, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you want a really deep dive int Go internals and become a better Go Developer then that's the book for you. Lots of interesting topics and internals for Go and very good examples/code snippets to understand every little detail and advanced topics.I really enjoyed the sections about data structures, advanced concurrency, testing and optimization.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.