Why Go? ======= Go_ is a application-oriented programming language developed by a programmers at Google. One of the major intended applications for Go_ is writing *servers*, and so much of the language is written to support this. Much of what follows is based on the `Go FAQ `_. This is a good starting place for learning about the design of Go_. Some of the major features of Go_ are: - **Fast compilation**. Go_ is designed from the ground up for writing large, multi-file programs. In languages like C and C++, compiling and linking multi-file programs is surprisingly time-consuming due in large part to the use of ``#define`` to combine files. Many large C++ programs at Google could not be efficiently built on a single computer, and so used a sophisticated distributed build system to make build times more reasonable. Apparently, the designers of Go_ got the idea for creating it during a 45 minute wait for a C++ compilation. - **Lightweight typing**. Go_ is a statically typed language, like C++ or Java. Yet this is done in such a way that it feels closer in spirit to non- statically typed languages, like Python or JavaScript. You can often avoid explicitly dealing with types. - **Novel use of interfaces**. Go_ is *object-oriented*, but it does not have classes or inheritance (at least in the sense of C++ or Java). Instead, interfaces and methods are combined to provide most of the same benefits of traditional object-oriented programming. - **Garbage collection**. As in Java or Python, their's no need to explicitly de-allocate memory. This simplifies code, and goes a long way towards stopping memory leaks. - **Closures**. Go_ supports anonymous functions (i.e. lambda functions), which are functions without names. Anonymous functions are allowed to refer to variables outside of themselves, which turns out to be an extremely powerful feature. - **Concurrency support**. Go_ uses channels and so-called "go routines" (essentially lightweight processes) to handle most concurrency tasks. These are based on a model of concurrency known as `communication sequential processes `_. - **No exceptions**. The designers of Go_ believe that exception systems in languages like C++ and Java ultimately lead to convoluted code that treats too many situations as errors (e.g. they believe that a failure to open a file should not be treated as an exception). Instead, Go_ relies on explicit error codes returned from functions, along with the functions ``defer``, ``panic``, and ``recover``. - **Pretty good standard tools and library.** Out of the box, Go_ comes with useful tools for things like source code formatting (``go fmt``) and testing (``go test``). It also has an extensive standard library with many practical packages. For instance, it is relatively easy to create a simple web-server in Go_ using just its standard library. Notes on Go ----------- Here are some notes introducing various basic features of Go_: - :doc:`Variables ` - :doc:`Control Structures: if statements, switch, and loops ` - :doc:`Functions ` - :doc:`Pointers ` - :doc:`Structures ` - :doc:`Slices and Arrays ` - :doc:`Maps ` - :doc:`Methods, Interfaces, and an Example of Sorting ` - Example programs: - :doc:`hello1` - :doc:`hello2` - :doc:`loop1` - :doc:`loop2` - :doc:`slice1` - :doc:`slice2` (slice notation) - :doc:`slice3` (slices refer to an underlying array) - :doc:`slice4` (using ``make``) - :doc:`numstats1` (includes simple file reading, and functions) - :doc:`numstats2` (includes simple file reading, and functions) - :doc:`a Go version of cat -n ` (read/write files, errors, ``defer``) - :doc:`a simple scanner ` (includes ``map``, ``iota``, ``switch``) - :doc:`types1` (new type with ``type``) - :doc:`types2` (structs, methods) - :doc:`types3` (the empty interface ``interface{}``, type switch, type assertion) - :doc:`types4` (example of an interface) - :doc:`functions1` (example of passing a function as a value; also shows table-driven testing) - :doc:`functions2` (a function that returns a function: asking a question) - :doc:`functions3` (a function that returns a function: numerical derivative) - :doc:`functions4` (the map function in Go_, and why it doesn't work well) - :doc:`functions5` (example of returning and using a closure)