Elementary default to a zero-value:
- int
- float
- bool
- string
Structured (or composite) default to nil value:
- struct
- array
- slice
- map
- channel
Interfaces - Only describe the behavior of a type
3 types of functions:
- Normal functions with an identifier
- Anonymous or lambda functions
- Methods
IDIOM
for i:=0; i<len(arr1); i++ {
arr1[i] = …
}
for i:=range arr1 {
...
}
NOTE: When assigning an array to another array, a copy is made!
- I think structs are the objects/classes of Go
- Methods are like prototypes in javascript. They are defined outside of the struct
- Interfaces are named collections of method signatures. (whatever the hell that means). The example shows an interface with 2 methods. The structs we want to have this interface must implement these 2 methods (but not explicitly said). Then another method/function can use that interface as arguments and this allows a duck typing?? I'll have to research this further. further reading
- you call a go routine and they will start work asynchronously (like javascript does naturally)
- channels allow your go routines to talk to one another. It allows for coordination. Kind of make it synchronous? create a channel using
make(chan val-type
messages := make(chan val-type)
go func() { messages <- "ping" }() // extra () calls it?
msg := <-messages // take the message from channel and assign it
Very key quote about Channel Buffering:
By default channels are unbuffered, meaning that they will only accept sends (chan <-) if there is a corresponding receive (<- chan) ready to receive the sent value. Buffered channels accept a limited number of values without a corresponding receiver for those values.
Another aspect of the Go type system that I love is Go's notion of interfaces. Interfaces are implemented implicitly. There is no "implements" keyword. Any type that defines the methods listed in a given interface type implements that interface by definition. This means that you can define interfaces that are implemented by code in the standard library, or in some other third party code that you don’t want to fork. The result is duck-typing that is compiler-enforced rather than simply a convention.