-
Notifications
You must be signed in to change notification settings - Fork 0
Kevin Kredit edited this page Jan 29, 2020
·
5 revisions
Notes and lessons from the Go programming language.
- Declared (with
var,:=, orconst) - Always initialized (explicitly, or implicitly to 0)
- Mutable by default
- Static
- Declared or inferred
- Context dependent for
consts - Discernable at runtime via
someVar.(type)
- Supports "blank identifiers"
_to ignore types and return values - Has C-like pointers
- Scope
- Local variables survive scope of functions
- Compiled, though
go runis 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)
- Several built-in tools for things like
- Channels are FIFOs between goroutines
- Send and receive are blocking by default
-
returnis mandatory; expressions != statements - May have multiple return values
- May have variadic arguments
- Are first class objects
- May be anonymous
- May form closures
- Supports recursion
- Add "methods" on struct types; like methods on classes
- "Interfaces" are named groups of methods
- 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
-
erroris 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
- 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?
- Why no
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.