ptg7913109
Introduction 3
bookkeeping by programmers to make sure they lock and unlock resources at
the right times.
The Go compiler and runtime system takes care of the tedious bookkeeping. For
memory management Go has a garbage collector, so there’s no need for smart
pointers or for manually freeing memory. And for concurrency, Go provides a
form of CSP (Communicating Sequential Processes) based on the ideas of com-
puter scientist C. A. R. Hoare, that means that many concurrent Go programs
don’t need to do any locking at all. Furthermore, Go uses goroutines—very
lightweight processes which can be created in vast numbers that are automati-
cally load-balanced across the available processors and cores—to provide much
more fine-grained concurrency than older languages’ thread-based approach-
es. In fact, Go’s concurrency support is so simple and natural to use that when
porting single-threaded programs to Go it often happens that opportunities for
using concurrency arise that lead to improved runtimes and better utilization of
machine resources.
Go is a pragmatic language that favors efficiency and programmer convenience
over purity. For example, Go’s built-in types and user-defined types are not the
same, since the former can be highly optimized in ways the latter can’t be. Go
also provides two fundamental built-in collection types: slices (for all practical
purposes these are references to variable-length arrays) and maps (key–value
dictionaries or hashes). These collection types are highly efficient and serve
most purposes extremely well. However, Go supports pointers (it is a fully com-
piled language—there’s no virtual machine getting in the way of performance),
so it is possible to create sophisticated custom types, such as balanced binary
trees, with ease.
While C supports only procedural programming and Java forces programmers
to program everything in an object-oriented way, Go allows programmers to use
the paradigm best suited to the problem. Go can be used as a purely procedural
language, but also has excellent support for object-oriented programming. As
we will see, though, Go’s approach to object orientation is radically different
from, say, C
++
, Java, or Python—and is easier to use and much more flexible
than earlier forms.
Like C, Go lacks generics (templates in C
++
-speak); however, in practice the
other facilities that Go provides in many cases obviate the need for generics.
Go does not use a preprocessor or include files (which is another reason why it
compiles so fast), so there is no need to duplicate function signatures as there is
in C and C
++
. And with no preprocessor, a program’s semantics cannot change
behind a Go programmer’s back as it can with careless #defines in C and C
++
.
Arguably, C
++
, Objective-C, and Java have all attempted to be better Cs (the
latter indirectly as a better C
++
).Go can also be seen as an attempt to be a better
C, even though Go’sclean,light syntax is reminiscent of Python—and Go’sslices
and maps are very similar to Python’s lists and dicts. However, Go is closer in