Why Go?

Go is an application-oriented programming language developed by team at Google. They wanted a modern language that was easy for new programmers to learn, and would, among other things, be good for writing servers.

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 can’t be efficiently built on a single computer, and so sophisticated distributed build systems are used 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, there’s no need to explicitly de-allocate memory. This simplifies code, and goes a long way towards stopping memory leaks and preventing dangling pointers.

  • Closures. Go supports anonymous functions (i.e. lambda functions), which are functions without names. Functions can be passed as parameters to other functions, and can functions can return functions. In particular, if a returned function refers to a variable defined locally in the function that returns it, Go handles the variable properly.

  • 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.

The Go FAQ is a good starting place for learning about the design of Go.

Hello, Go!

Here is the basic “Hello, world!” program in Go:

// hello.go

//
// To run this, type the following at the command-line:
//
//   $ go run hello.go
//

package main

import "fmt"

func main() {
    fmt.Println("Hello from Go!")
}

A few thing to notice:

  • Go code is organized into packages, and a runnable Go program must have one package called main, and within that it must have a function named main. When a Go program runs, the main() in the main is the first function that gets called.
  • fmt is a package in the Go standard library that contains basic reading and writing functions such as fmt.Println.
  • Code statements can end with a ;, but don’t need to. In practice, they are rarely used in Go.
  • All function definitions start with func.
  • Source code comments work as in C/C++, i.e. // is a single-line comment, and everything between /* and */ is a comment.
  • Go is a compiled language, and we will be mainly using the go run command to compile and run our programs in one step. Go usually compiles quickly, and so using go run can make Go feel almost as interactive as an interpreted language at times.
  • The command go fmt can be used to format a Go program in a standard way, which greatly increases code readability. Many Go editors will automatically call go fmt every time you save your code.

Notes on Go

Here are some notes introducing various basic features of Go: