CODE LESS, COMPILE QUICKER, EXECUTE FASTER => HAVE MORE FUN DEVELOPING!
This text presents the most comprehensive treatment of the Go programming language you can find. It draws on the whole spectrum of Go sources available: online documentation and blogs, books, articles, audio and video, and my own experience in software engineering and teaching programming languages and databases, organizing the concepts and techniques in a systematic way.
Several researchers and developers at Google experienced frustration with the software development processes within the company, particularly using C++ to write large server software. The binaries tended to be huge and took a long time to compile, and the language itself was quite old. A lot of the ideas and changes in hardware that have come about in the last couple of decades haven't had a chance to influence C++. So the researchers sat down with a clean sheet of paper and tried to design a language that would solve the problems they had:
1. software needs to built quickly,
2. the language should run well on modern multi-core hardware,
3. the language should work well in a networked environment,
4. the language should be a pleasure to use.
And so was born "Go", a language that has the feel of a dynamic language like Python or Ruby, but has the performance and safety of languages like C or Java.
Go seeks to reinvent programming in the most practical of ways: its not a fundamentally new language with strange syntax and arcane concepts; instead it builds and improves a lot on the existing C/Java/C#-style syntax. It proposes interfaces for object-oriented programming and goroutines / channels for concurrent and parallel programming.
This book is intended for developers who want to learn this new, fascinating and promising language. Some basic knowledge of programming and some experience with a programming language and environment is assumed, but a thorough knowledge of C or Java or the like is not needed.
For those of you who are familiar with C or the current object oriented languages, we will compare the concepts in Go with the corresponding concepts in these languages (throughout the book we will use the well known OO abrevation, to mean object-oriented).
This text explains everything from the basic concepts onwards, but at the same time we discuss advanced concepts and techniques such as a number of different patterns when applying goroutines and channels, how to use the google api from Go, how to apply memoization, how to use testing in Go and how to use templating in web applications.
In Part I we discuss the origins of the language (ch 1) and get you started with the installation of Go (ch 2) and a development environment (ch 3).
Part 2 then guides you through the core concepts of Go: the simple and composite types (ch 4, 7, 8), control structures (ch 5), functions (ch 6), structs with their methods (ch 10), and interfaces (ch 11). The functional and object-oriented aspects of Go are thoroughly discussed, as well as how Go code in larger projects is structured (ch 9).
Part 3 learns you how to work with files in different formats (ch 12) and how to leverage the error-handling mechanism in Go (ch 13). It also contains a thorough treatment of Go’s crown jewel: goroutines and channels as basic technique for concurrent and multicore applications (ch 14). Then we discuss the networking techniques in Go and apply this to distributed and web applications (ch 15).
Part 4 shows you a number of Go language patterns and idioms (ch 16, 17), together with a collection of useful code snippets (ch 18). With all of the techniques which you have learned in the previous chapters, a complete Go project is built (ch 19) and you get an introduction in how to use Go in the cloud (Google App Engine) (ch 20). In the last chapter (ch 21) we discuss some real world uses of go in businesses and organizations all over the world. The text is concluded with quotes of users, listings, references to Go packages and tools, answers to questions and exercises, and a bibliography of all resources and references.
Go has very much a ‘no nonsense’ approach to it: extreme care has gone into making things easy and automatic; it adheres to the KISS principle from Agile programming: Keep It Short and Simple!
Solving or leaving out many of the ‘open’ features in C, C++ or Java makes the developer’s life much easier! A few examples are: default initializations of variables; memory is allocated and freed automatically; fewer, but more powerful control constructs. As we will see Go also aims to prevent unnecessary typing, often Go code is shorter and easier to read than code from the classic object-oriented languages.
Go is simple enough to fit in your head, which can’t be said from C++ or Java; the barrier to entry is low, compared to e.g. Scala (the Java concurrent language). Go is a modern day C.
Most of the code-examples and exercises provided interact with the console, which is not a surprise since Go is in essence a systems language. Providing a graphical user interface (GUI) framework which is platform-independent is a huge task. Work is under way in the form of a number of 3rd party projects, so somewhere in the near future there probable will be a usable Go GUI framework. But in this age the web and its protocols are all pervasive, so to provide a GUI in some examples and exercises we will use Go’s powerful http and template packages.
We will always use and indicate what is called idiomatic Go-code, by which we mean code which is accepted as being best practice. We try to make sure that examples never use concepts or techniques which were not covered up until that point in the text. There are a few exceptions where it seemed better to group it with the discussion of the basic concept: in that case the advanced concept is referenced and the § can be safely skipped.
All concepts and techniques are thoroughly explained through 227 working code examples (on a grey background), printed out and commented in the text and available online for execution and experimenting.
The book is cross-referenced as much as possible, forward as well as backward. And of course this is what you must do: after setting up a Go environment with a decent editor, start experimenting with the code examples and try the exercises: mastering a new language and new concepts can only be achieved through exercising and experimenting, so the text contains 130 exercises, with downloadable solutions. We have used the famous Fibonacci algorithm in examples and exercises in 13 versions to illustrate different concepts and coding techniques in Go.
The book has an website (https://sites.google.com/site/thewaytogo2012/) from where the code examples can be downloaded and on which complementary material and updates are available.
For your convenience and further paving your path to become a Go master, special chapters are dedicated to the best practices and language patterns in Go, and another to the pitfalls for the Go beginner. Handy as a desktop-reference while coding is chapter 18, which is a collection of the most useful code snippets, with references to the explanations in the text.
And last but not least, a comprehensive index leads you quickly to the page you need at the moment. All code has been tested to work with the stable Go-release Go 1.
Here are the words of Bruce Eckel, a well known authority on C++, Java and Python:
“ Coming from a background in C/C++, I find Go to be a real breath of fresh air. At this point, I think it would be a far better choice than C++ for doing systems programming because it will be much more productive and it solves problems that would be notably more difficult in C++. This is not to say that I think C++ was a mistake -- on the contrary, I think it was inevitable. At the time, we were deeply mired in the C mindset, slightly above assembly language and convinced that any language construct that generated significant code or overhead was impractical. Things like garbage collection or language support for parallelism were downright ridiculous and no one took them seriously. C++ took the first baby steps necessary to drag us into this larger world, and Stroustrup made the right choices in making C++ comprehensible to the C programmer, and able to compile C. We needed that at the time.
We've had many lessons since then. Things like garbage collection and exception handling and virtual machines, which used to be crazy talk, are now accepted without question. The complexity of C++ (even more complexity has been added in the new C++), and the resulting impact on productivity, is no longer justified. All the hoops that the C++ programmer had to jump through in order to use a C-compatible language make no sense anymore -- they're just a waste of time and effort. Now, Go makes much more sense for the class of problems that C++ was originally intended to solve.”
I would like to express my sincere gratitude to the Go team for creating this superb language, especially “Commander” Rob Pike, Russ Cox and Andrew Gerrand for their beautiful and illustrative examples and explanations. I also thank Miek Gieben, Frank Müller, Ryanne Dolan and Satish V.J. for the insights they have given me, as well as countless other members of the Golang-nuts mailing list.
Welcome to the wonderful world of developing in Go!
1-1 of 1