Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Mastering Go
Mastering Go

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

Arrow left icon
Profile Icon Mihalis Tsoukalos
Arrow right icon
Mex$179.99 Mex$1082.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1 (33 Ratings)
eBook Aug 2019 798 pages 2nd Edition
eBook
Mex$179.99 Mex$1082.99
Paperback
Mex$1353.99
Subscription
Free Trial
Arrow left icon
Profile Icon Mihalis Tsoukalos
Arrow right icon
Mex$179.99 Mex$1082.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1 (33 Ratings)
eBook Aug 2019 798 pages 2nd Edition
eBook
Mex$179.99 Mex$1082.99
Paperback
Mex$1353.99
Subscription
Free Trial
eBook
Mex$179.99 Mex$1082.99
Paperback
Mex$1353.99
Subscription
Free Trial

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

Go and the Operating System

This chapter is an introduction to various Go topics that beginners will find very useful. More experienced Go developers can also use this chapter as a refresher course on the fundamentals of Go. As it happens with most practical subjects, the best way to understand something is to experiment with it. In this case, experimenting means writing Go code on your own, making your own mistakes, and learning from them! Just don't let error messages and bugs discourage you.

In this chapter, you will learn about:

  • The history and the future of the Go programming language
  • The advantages of Go
  • Compiling Go code
  • Executing Go code
  • Downloading and using external Go packages
  • UNIX standard input, output, and error
  • Printing data on the screen
  • Getting user input
  • Printing data to standard error
  • Working with log files
  • Using Docker to compile and execute a Go source file
  • Error handling in Go

The history of Go

Go is a modern, generic-purpose, open source programming language that was officially announced at the end of 2009. Go began as an internal Google project, which means that it was started as an experiment, and has since been inspired by many other programming languages, including C, Pascal, Alef, and Oberon. Go's spiritual fathers are the professional programmers Robert Griesemer, Ken Thomson, and Rob Pike.

They designed Go as a language for professional programmers who want to build reliable, robust, and efficient software. Apart from its syntax and its standard functions, Go comes with a pretty rich standard library.

At the time of writing, the latest stable Go version is version 1.13. However, even if your version number is higher, the contents of the book will still be relevant.

If you are going to install Go for the first time, you can start by visiting https://golang.org/dl/. However, there is a big chance that your UNIX variant has a ready-to-install package for the Go programming language, so you might want to get Go by using your favorite package manager.

Where is Go going?

The Go community is already discussing the next major version of Go, which is going to be called Go 2, but there is nothing definitive at the moment.

The intention of the current Go 1 team is to make Go 2 more community driven. Although this is a good idea in general, it is always dangerous when lots of people try to make important decisions about a programming language that was initially designed and developed as an internal project by a small group of great people.

Some of the big changes that are being considered for Go 2 are generics, package versioning, and improved error handling. All these new features are under discussion at the moment and you should not be worried about them, but it is worthwhile to have an idea of the direction that Go is going in.

The advantages of Go

Go has many advantages, and some of them are unique to Go, while others are shared with other programming languages.

The list of the most significant Go advantages and features includes the following:

  • Go is a modern programming language that is easy to read, easy to understand, and was made by experienced developers.
  • Go wants happy developers because happy developers write better code!
  • The Go compiler prints practical warning and error messages that help you to solve the actual problem. Putting it simply, the Go compiler is there to help you, not to make your life miserable by printing pointless output!
  • Go code is portable, especially among UNIX machines.
  • Go has support for procedural, concurrent, and distributed programming.
  • Go supports garbage collection, so you do not have to deal with memory allocation and deallocation.
  • Go does not have a preprocessor and does high-speed compilation. As a consequence, Go can also be used as a scripting language.
  • Go can build web applications and provides a simple web server for testing purposes.
  • The standard Go library offers many packages that simplify the work of the developer. Additionally, the functions found in the standard Go library are tested and debugged in advance by the people who develop Go, which means that, most of the time, they come without bugs.
  • Go uses static linking by default, which means that the binary files produced can be easily transferred to other machines with the same OS. As a consequence, once a Go program is compiled successfully and an executable file is generated, you do not need to worry about libraries, dependencies, and different library versions anymore.
  • You will not need a graphical user interface (GUI) for developing, debugging, and testing Go applications, as Go can be used from the command-line, which I think many UNIX people prefer.
  • Go supports Unicode, which means that you do not need any extra code for printing characters from multiple human languages.
  • Go keeps concepts orthogonal because a few orthogonal features work better than many overlapping ones.

Is Go perfect?

There is no such thing as the perfect programming language, and Go is not an exception to this rule. However, some programming languages are better at some areas of programming or we like them more than other programming languages. Personally, I do not like Java, and while I used to like C++, I do not like it anymore. C++ has become too complex as a programming language, whereas, in my opinion, Java, code does not look good.

Some of the disadvantages of Go are:

  • Go does not have direct support for object-oriented programming, which can be a problem for programmers who are used to writing code in an object-oriented manner. Nevertheless, you can use composition in Go to mimic inheritance.
  • For some people, Go will never replace C.
  • C is still faster than any other programming language for systems programming and this is mainly because UNIX is written in C.

Nevertheless, Go is a pretty decent programming language that will not disappoint you if you find the time to learn it and program in it.

What is a preprocessor?

I said earlier that Go does not have a preprocessor and that this is a good thing. A preprocessor is a program that processes your input data and generates output that will be used as the input to another program. In the context of programming languages, the input of a preprocessor is source code that will be processed by the preprocessor before being given as input to the compiler of the programming language.

The biggest disadvantage of a preprocessor is that it knows nothing about the underlying language or its syntax! This means that when a preprocessor is used, you cannot be certain that the final version of your code will do what you really want because the preprocessor might alter the logic as well as the semantics of your original code.

The list of programming languages with a preprocessor includes C, C++, Ada, and PL/SQL. The infamous C preprocessor processes lines that begin with # and are called directives or pragmas. As stated before, directives and pragmas are not part of the C programming language!

The godoc utility

The Go distribution comes with a plethora of tools that can make your life as a programmer easier. One of these tools is the godoc utility, which allows you to see the documentation of existing Go functions and packages without needing an internet connection.

The godoc utility can be executed either as a normal command-line application that displays its output on a terminal, or as a command-line application that starts a web server. In the latter case, you will need a web browser to look at the Go documentation.

If you type godoc without any command-line parameters, you will get a list of the command-line options supported by godoc.

The first way is similar to using the man(1) command, but for Go functions and packages. So, in order to find information about the Printf() function of the fmt package, you should execute the following command:

$ go doc fmt.Printf
  

Similarly, you can find information about the entire fmt package by running the following command:

$ go doc fmt
  

The second way requires executing godoc with the -http parameter:

$ godoc -http=:8001
  

The numeric value in the preceding command, which in this case is 8001, is the port number the HTTP server will listen to. You can choose any port number that is available provided that you have the right privileges. However, note that port numbers 0-1023 are restricted and can only be used by the root user, so it is better to avoid choosing one of those and pick something else, provided that it is not already in use by a different process.

You can omit the equal sign in the presented command and put a space character in its place. So, the following command is completely equivalent to the previous one:

$ godoc -http :8001
  

After that, you should point your web browser to the http://localhost:8001/pkg/ URL in order to get the list of available Go packages and browse their documentation.

Compiling Go code

In this section, you will learn how to compile Go code. The good news is that you can compile your Go code from the command line without the need for a graphical application. Furthermore, Go does not care about the name of the source file of an autonomous program as long as the package name is main and there is a single main() function in it. This is because the main() function is where the program execution begins. As a result, you cannot have multiple main() functions in the files of a single project.

We will start our first Go program compilation with a program named aSourceFile.go that contains the following Go code:

package main 
  
import ( 
    "fmt" 
) 
 
func main() { 
    fmt.Println("This is a sample Go program!") 
} 

Notice that the Go community prefers to name the Go source file source_file.go instead of aSourceFile.go. Whatever you choose, be consistent.

In order to compile aSourceFile.go and create a statically linked executable file, you will need to execute the following command:

$ go build aSourceFile.go
  

After that, you will have a new executable file named aSourceFile that you will need to execute:

$ file aSourceFile
aSourceFile: Mach-O 64-bit executable x86_64
$ ls -l aSourceFile
-rwxr-xr-x  1 mtsouk  staff  2007576 Jan 10 21:10 aSourceFile
$ ./aSourceFile
This is a sample Go program!
  

The main reason why the file size of aSourceFile is that big is because it is statically linked, which means that it does not require any external libraries to run.

Executing Go code

There is another way to execute your Go code that does not create any permanent executable files – it just generates some intermediate files that are automatically deleted afterward.

The way presented allows you to use Go as if it is a scripting programming language like Python, Ruby, or Perl.

So, in order to run aSourceFile.go without creating an executable file, you will need to execute the following command:

$ go run aSourceFile.go
This is a sample Go program!
  

As you can see, the output of the preceding command is exactly the same as before.

Please note that with go run, the Go compiler still needs to create an executable file. It is because you do not see it, it is automatically executed, and it is automatically deleted after the program has finished that you might think that there is no need for an executable file.

This book mainly uses go run to execute the example code; primarily because it is simpler than running go build and then executing the executable file. Additionally, go run does not leave any files on your hard disk after the program has finished its execution.

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
$19.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
$199.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 Mex$85 each
Feature tick icon Exclusive print discounts
$279.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 Mex$85 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Mex$ 3,159.97
Go Programming Cookbook
Mex$902.99
The Go Workshop
Mex$902.99
Mastering Go
Mex$1353.99
Total Mex$ 3,159.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.