Observations about Go language
Having coded in C*, F#, Java, Groovy, and Python, here are my observations about Go after reading The Way to Go and Go in Action books.
- main() function defined in main package serves as the entry point to a Go program.
- Each source file can have a init() function that is executed once before main() function in the package.
- Types appear after identifiers.
- Type aliases are supported.
- Go is both strongly and statically typed.
- Top-level identifiers with names starting with Capital letters are exported from the containing package. This is true of struct members.
- := operator creates and initializes variables.
- Multiple variable assignment is supported.
- Complex numbers are supported via primitive types.
- Strings come in two flavors: interpreted (“”) and raw (‘’).
- Pointers (*) and references (&) are supported
- Pointer arithmetic is not supported.
- Unlike in C or Java, control does not implicitly fall thru between case blocks in a switch statement (unless fallthrough statement is used).
- switch statement can decide on both values and types. It can also serve as complex if statement.
- for loop comes in three forms: counter-controlled (as usual), condition-controlled (while loops), and enumerative (iterator-based loops).
- Like C, break, continue, and goto statements are supported with labels.
- Return type of a function appears after the function signature.
- Functions are first-class values, i.e., they can be assigned to variables.
- Functions support multiple return values and _ is used to ignore values.
- Functions support named return variables.
- Instead of exceptions, Go has defer, panic, and recover mechanisms (like with statement context managers, raise, and except in Python).
- new and make operators are used to allocate memory on heap for value types and reference types, respectively.
- Arrays are fixed length sequence of homogeneous values. [Value Type]
- Slices are modifiable “views” of arrays that cannot grow beyond the capacity of the underlying array. [Ref Type]
- By default, Arrays are passed by value to functions while slices, map, structures, interfaces, and channels are passed by reference.
- Argument unpacking (aka splatting) is supported via … operator.
- Maps are supported. [Ref Type]
- Using arrays, structures, and structs values as keys in maps is a bit tedious (at least for folks coming from Java or C#).
- Structs are like structures in C with support to associate methods to them. [Value Type]
- Anonymous (no name) fields are supported. This kinda simulates inheritance via embedding (nameless composition).
- Functions (aka Methods) can be associated with any type (including function types and primitive types) except interfaces (kinda like extension methods in C#).
- Method receiver (this) parameter is explicitly declared like self in Python.
- Method and its receiver type should be defined in the same package.
- Methods do not carry over to aliases of receiver types.
- Types implicitly satisfy interfaces if they implement interface methods (as in structural typing).
- It has channels!! It has channels!! It has channels!! :)
- Dependency management and build system is a bit archaic and complex when compared to systems such as Maven and Gradle.
- Development tools (e.g., formatting, linting, testing, benchmarking) are basic yet well thought out.
Now, for some coding in Go :)