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

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.

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.