Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Go - Fourth Edition

You're reading from  Mastering Go - Fourth Edition

Product type Book
Published in Mar 2024
Publisher Packt
ISBN-13 9781805127147
Pages 736 pages
Edition 4th Edition
Languages
Author (1):
Mihalis Tsoukalos Mihalis Tsoukalos
Profile icon Mihalis Tsoukalos
Toc

Table of Contents (19) Chapters close

Preface 1. A Quick Introduction to Go 2. Basic Go Data Types 3. Composite Data Types 4. Go Generics 5. Reflection and Interfaces 6. Go Packages and Functions 7. Telling a UNIX System What to Do 8. Go Concurrency 9. Building Web Services 10. Working with TCP/IP and WebSocket 11. Working with REST APIs 12. Code Testing and Profiling 13. Fuzz Testing and Observability 14. Efficiency and Performance 15. Changes in Recent Go Versions 16. Other Books You May Enjoy
17. Index
Appendix: The Go Garbage Collector

When to use Go

Although Go is a general-purpose programming language, it is primarily used for writing system tools, command line utilities, web services, and software that works over networks and the internet. You can use Go for teaching programming, and it is a good candidate as your first programming language because of its lack of verbosity and clear ideas and principles.

Go can help you develop the following kinds of applications:

  • Professional web services
  • Networking tools and servers such as Kubernetes and Istio
  • Backend systems
  • Robust UNIX and Windows system tools
  • Servers that work with APIs and clients that interact by exchanging data in myriad formats, including JSON, XML, and CSV
  • WebSocket servers and clients
  • gRPC (Remote Procedure Call) servers and clients
  • Complex command line utilities with multiple commands, sub-commands, and command line parameters, such as docker and hugo
  • Applications that exchange data in the JSON format
  • Applications that process data from relational databases, NoSQL databases, or other popular data storage systems
  • Compilers and interpreters for your own programming languages
  • Database systems such as CockroachDB and key/value stores such as etcd

Although Go is a very practical and competent programming language, it is not perfect:

  • This is a personal preference rather than an actual technical shortcoming: Go has no direct and full support for object-oriented programming, which is a popular programming paradigm.
  • Although goroutines are lightweight, they are not as powerful as OS threads. Depending on the application you are trying to implement, there might exist some rare cases where goroutines will not be appropriate for the job. The Apache web server creates UNIX processes with fork(2) to serve its clients—Go does not support the functionality of fork(2). However, in most cases, designing your application with goroutines and channels in mind will solve your problems.
  • Although garbage collection is fast enough most of the time, and for almost all kinds of applications, there are times when you need to handle memory allocation manually, such as when developing an operating system or working with large chunks of memory and want to avoid fragmentation—Go cannot do that. In practice, this means that Go will not allow you to perform any memory management manually.
  • Go does not offer the full functionality of a functional programming language.
  • Go is not good at developing systems with high availability guarantees. In such cases, use Erlang or Elixir instead.

There are many things that Go does better than other programming languages, including the following:

  • The Go compiler catches a large set of silly errors that might end up being bugs. This includes imported Go packages and variables that are not being used in the code.
  • Go uses fewer parentheses than C, C++, or Java, and no semicolons, which makes Go source code more human-readable and less error-prone.
  • Go comes with a rich and reliable standard library that keeps improving.
  • Go has support for concurrency out of the box through goroutines and channels.
  • Goroutines are lightweight. You can easily run thousands of goroutines on any modern machine without any performance issues.
  • Unlike C, Go considers functions as first-class citizens.
  • Go code is backward compatible, which means that newer versions of the Go compiler accept programs that were created using a previous version of the language without any modifications. This compatibility guarantee is limited to major versions of Go. For example, there is no guarantee that a Go 1.x program will compile with Go 2.x.

The next subsection describes my personal Go journey.

My personal Go journey

In this subsection, I am going to tell you my personal story of how I ended up learning and using Go. I am a UNIX person, which means that I like UNIX and prefer to use it whenever possible. I also love C, and I used to like C++; I wrote a command line FTP client in C++ for my M.Sc. project. Nowadays, C++ is just a huge programming language that is difficult to learn. Although C continues to be a decent programming language, it requires lots of code to perform simple tasks and suffers from difficult-to-find and correct bugs, due to manual memory management and extremely flexible conversion between different data types without any warnings or error messages.

As a result, I used to use Perl to write simple command line utilities. However, Perl is far from perfect for writing serious command line tools and services, as it is a scripting programming language and is not intended for web development.

When I first heard about Go, that it was developed by Google, and that both Rob Pike and Ken Thomson were involved in its development, I instantly became interested in Go.

Since then, I have used Go to create web services, servers, and clients that communicate with RabbitMQ, MySQL, and PostgreSQL, create simple command line utilities, implement algorithms for time series data mining, create utilities that generate synthetic data, etc.

Soon, we are going to move on to actually learn some Go, using Hello World! as the first example, but before that, we will present the go doc command, which allows you to find information about the Go standard library, its packages, and their functions, as well as the godoc utility.

If you have not already installed Go, this is the right time to do so. To do that, visit https://go.dev/dl/ or use your favorite package manager.

The go doc and godoc utilities

The Go distribution comes with a plethora of tools that can make your life as a programmer easier. Two of these tools are the go doc subcommand and godoc utility, which allow you to see the documentation of existing Go functions and packages without needing an internet connection. However, if you prefer viewing the Go documentation online, you can visit https://pkg.go.dev/.

The go doc command can be executed as a normal command line application that displays its output on a terminal, and it is similar to the UNIX man(1) command, but for Go functions and packages only. So, in order to find out information about the Printf() function of the fmt package, you should execute the following command:

$ go doc fmt.Printf

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

$ go doc fmt

As godoc is not installed by default, you might need to install it by running go install golang.org/x/tools/cmd/godoc@latest. The godoc binary is going to be installed in ~/go/bin, and you can execute it as ~/go/bin/godoc unless ~/go/bin is in your PATH environment variable.

The godoc command line application starts a local web server. So you need a web browser to look at the Go documentation.

Running godoc requires executing godoc with the -http parameter:

$ ~/go/bin/godoc -http=:8001

The numeric value in the preceding command, which in this case is 8001, is the port number that the HTTP server will listen to. As we have omitted the IP address, godoc is going to listen to all network interfaces.

You can choose any port number that is available if you have the right privileges. However, note that port numbers 01023 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, if it is not already in use by a different process. Port number 8001 is usually free and is frequently used for local HTTP servers.

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

$ ~/go/bin/godoc -http :8001

After that, you should point your web browser to http://localhost:8001/ in order to get the list of available Go packages and browse their documentation. If no -http parameter is provided, godoc listens to port 6060.

If you are using Go for the first time, you will find the Go documentation very handy for learning the parameters and the return values of the functions you want to use — as you progress in your Go journey, you will use the Go documentation to learn the gory details of the functions and variables that you want to use.

The next section presents the first Go program of the book and explains the basic concepts of Go.

You have been reading a chapter from
Mastering Go - Fourth Edition
Published in: Mar 2024 Publisher: Packt ISBN-13: 9781805127147
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 $15.99/month. Cancel anytime