Go Practices 101
Theme: dark/light
Three new books, Go Optimizations 101,
Go Details & Tips 101
and Go Generics 101
are published now.
It is most cost-effective to buy all of them through this book bundle
in the Leanpub book store.
Go is a compiled and static typed programming language born from Google. Many of the core Go design and tool development team members have many years of experience in the field of programming language research.
Go has many features. Some are unique, some are borrowed from other programming languages:
-
built-in concurrent programming support
-
the container types map
and slice
are first-class citizens.
-
polymorphism through interfaces.
-
value boxing and reflection through interfaces.
-
pointers.
-
function closures.
-
methods.
-
deferred function calls.
-
type embedding.
-
type deduction.
-
memory safety.
-
automatic garbage collection.
-
great cross-platform compatibility.
-
custom generics (since Go 1.18).
Besides the above features, further highlights are:
-
The syntax of Go is deliberately designed to be simple, clean, and similar to other popular programming languages. This makes Go programming easy to pick up.
-
Go comes with a great set of standard code packages which provide all kinds of common functionalities. Most of the packages are cross-platform.
-
Go also has an active community, and there are plenty of high quality third party Go libraries and projects to import and use.
Go programmers are often called gophers.
In fact, although Go is a compiled and static typed programming language, Go also has many features which are usually only available in dynamic script languages. It is hard to combine these two kinds into one language, but Go did it. In other words, Go owns both the strictness of static languages and the flexibility of dynamic languages. I can't say there are not any compromises between the two, but the effect of the compromises is much weaker than the benefits of the combination in Go.
Readability is an important factor which affects the design of Go heavily. It is not hard for a gopher to understand the Go code written by other gophers.
Currently, the most popular Go compiler is written in Go and maintained by the Go design team. Later we shall call it the standard Go compiler, or gc
(an abbreviation for Go compiler, not for garbage collection GC). The Go design team also maintains a second Go compiler, gccgo
. Nowadays it's use is less popular than gc
, but it always serves as a reference, and both compilers are in active development. As of now the Go team focuses on the improvement of gc
.
gc
is provided in Go Toolchain (a set of tools for Go development maintained by Go team). Go Toolchain 1.0 was released in March, 2012. The version of Go is consistent with the version of Go Toolchain. There were/are two major versions released each year.
Since the release of Go 1.0, the syntax of Go has changed a little, but there were/are many improvements for the tools in Go Toolchain, from version to version, especially for gc
. For example, noticeable lags caused by garbage collecting is a common criticism for languages with automatic memory management. But since Go 1.8, improvements made for the concurrent garbage collection implementation in gc
basically eliminated the lag problem.
gc
supports cross-platform compilation. For example, we can build a Windows executable on a Linux OS, and vice versa.
Programs written in go language mostly compile very fast. Compilation time is an important factor for the happiness in development. Short build time is one reason why many programmers like programming with Go.
Advantages of Go executables are:
Some other compiled languages, such as C/C++/Rust may also have these three advantages (and they may have their respective advantages compared to Go), but they lack three important characteristics of Go:
-
fast compilation results in happy local development experience and short deployment iteration cycles
-
flexible, like dynamic languages
-
built-in concurrent programming support
All the above advantages combined make Go an outstanding language and a good choice for many kinds of projects. Currently, Go is popularly used in network, system tools, database development and block chain development areas. With the introduction of custom generics in Go 1.18, it is expected that Go will be used more and more in some other areas, such as gui/game, big data and AI.
Finally, Go is not perfect in all aspects. There are certain trade-offs in Go design. And Go really has some shortcomings. For example, Go doesn't support arbitrary immutable values now, which leads to that many values which are not intended to be modified in standard packages are declared as variables. This is a potential security weak point for some Go programs.
The digital versions of this book are available at the following places:
Tapir, the author of Go 101, has been on writing the Go 101 series books
and maintaining the go101.org website since 2016 July.
New contents will be continually added to the book and the website from time to time.
Tapir is also an indie game developer.
You can also support Go 101 by playing
Tapir's games
(made for both Android and iPhone/iPad):
Individual donations via PayPal are also welcome.
Articles in this book:
- An Introduction of Go - why Go is worth learning.
- The Go Toolchain - how to compile and run Go programs.
-
Become Familiar With Go Code