Skip to content
Kevin Kredit edited this page Jan 29, 2020 · 5 revisions

Notes and lessons from the Go programming language.

Language Features

Data

Data

  • Declared (with var, :=, or const)
  • Always initialized (explicitly, or implicitly to 0)
  • Mutable by default

Types

  • Static
  • Declared or inferred
  • Context dependent for consts
  • Discernable at runtime via someVar.(type)

Variables

  • Supports "blank identifiers" _ to ignore types and return values
  • Has C-like pointers
  • Scope
    • Local variables survive scope of functions

Structure and Control

Execution

  • Compiled, though go run is a one-step compiler and runner that gives the feel of an interpreter
  • "Goroutines" are lightweight threads
    • Several built-in tools for things like
      • Synchonization (waitgroups)
      • Communication (channels)
  • Channels are FIFOs between goroutines
    • Send and receive are blocking by default

Functions

  • return is mandatory; expressions != statements
  • May have multiple return values
  • May have variadic arguments
  • Are first class objects
    • May be anonymous
    • May form closures
  • Supports recursion

Objects and Interfaces

  • Add "methods" on struct types; like methods on classes
  • "Interfaces" are named groups of methods

Error Handling

  • Idiomatic to return explicit extra error value
    • Benefit of handling errors without additional constructs like exceptions
    • Also benefit of not overloading a single return value, like C, or requiring return values as parameters
  • error is a built-in interface
  • Idiomatic to use inline error checks
    • Side note: I wonder how error handling can be made to look good, as in C here

Tooling

  • Default formatting tool, gofmt
  • Fails to compile be default with unused variables and packages
  • Library support is good, but missing some things I really expect
    • Why no pop() from slices?

Feel

Initial thoughts: feels like C with a lot of the annoying things taken out. Though it still has pointers and mutable-by-default data, it does feel less fragile than C. Perhaps that's because of its automatic handling of some drudgery, like pointer-object conversion, print format handling, etc. The addition of interfaces makes for a flexible and less annoying version of encapsulation and polymorphism than C++. Goroutines make multi-threaded programming a pleasure.

Clone this wiki locally