Go is a statically typed, compiled programming language. It’s designed primarily for ease of learning and for easily writing powerful concurrent code. Memory management is done via a garbage collector.
Basics
Go primarily organises and separates code with packages, such that each imported package (via import) only reads a single file. This is done by building a package metadata file that contains information about all of the file’s dependencies. This also means that Go’s compiler does less work importing everything.
Go’s type system is similar to C’s. Other than the usual Boolean, integer (uint8, int32, int for machine size), float, and complex number types, it also provides structs, fixed-sized arrays, and pointers. Built into the language are strings, hash table (map), and dynamic arrays (called slices). There are no classes in Go, but methods can be defined for any type (similar to Rust). There’s also no type hierarchy (i.e., no inheritance).
OOP-style polymorphism is enabled without needing inheritance. This is done via Go interfaces, which contain a list of method names and signatures (i.e., no implementation). Then, any Go type that has methods with the same names and signatures as an interface is considered to implement that interface (without needing it explicitly declared).
The way that Go performs encapsulation is via its naming convention. Names starting with an uppercase letter are exported (public), and others aren’t.
Go’s main feature are goroutines. They run in a single shared address space and are mapped onto operating system level threads. If a goroutine calls a blocking operation, goroutines will be mapped to that thread to keep running. Channels are the idiomatic way to synchronise between goroutines — they’re unidirectional, limited-sized pipes that carry typed messages. There are still classical synchronisation schemes (mutexes, condition variables, semaphores) in the standard library.
goroutines
defer— will run expr at end of function. guaranteed even for multiplereturns
Tooling
The primary tool is go — it functions as the compiler, tester, formatter, and linter (among others, quite like Cargo). Some commands (prefixed with go):
run <FILE>.go— runs a particular file.build <FILE>.go— compiles a file to an executable.
Language features
Resources
- Introductory resources
- The Go Programming Language and Environment, by Russ Cox, Robert Griesemer, Rob Pike, Ian Lance Taylor, and Ken Thompson
- How to Write Go Code
- Go by Example
- Effective Go
- More advanced resources
- Distributed Services with Go, by Travis Jeffery