Go Basics
Variables
Strictly Typed and Compiled
Functions
function singature func functionname(parametername type) returntype {
hello world
Composite Data Types
Arrays
Arrays are fixed length
ArrayType = [length]ElementType
Slices
Numbered lists of a single type. They can be resized. Slices are built on top of arrays and passed by reference
Composite literal syntax t := type{values}
Iterate through slice
Working with Slices
Make function will create a new slice
Shorthand for creating a new slice and iterating through it
Append items to slice.
... will unfurl a list. It is useful for passing in variadic parameters from list data types (like above.)
Slicing a slice
Deleting from a slice
Maps
hash table data type
MAPS CAN ONLY BE INITIATED USING make() or A LITERAL.
cannot append to a zero valued maps!
Check if map value exist
the second value ok is a bool that is true if key exists in map
Maps Passed by reference
Maps are passed by reference;
i.e. when passed to a function, operations are performed on the underlying map
Basic Struct
collections of fields
Nested Struct
Structs can be nested
Embed Struct
Fake (data-only) inheritence
Struct Methods
structs can have methods. methods are funcs that have a receiver
Interfaces
A set of method signatures. A type "implements" an interface just by having those methods
Concurrency
Fatal errors and defer
When relying on defer to close open connections, close waitgroups, or clear buffered channels be careful with how error handling is approached. For example:
log.Fatalf() calls os.Exit() which exits the current program and returns a status code 0-125. This will skip defer, panic(), and any other resource cleanup.
Maps
Obviously writing to maps isn't safe for conncurrency and a mutex lock should be used in this case. Reading can be thread safe, but only if it assured that no modifications could be performed while a thread is attempting to read. If thread is attempting to read a map while it is updated by another thread, this will result in a panic().
In a purely "hypothetical" example
This can be solved by implementing logic to ensure that reading is a thread safe operation. e.g.