Skip to content

Instantly share code, notes, and snippets.

@jameshwang
Created April 8, 2013 08:00
Show Gist options
  • Save jameshwang/5335044 to your computer and use it in GitHub Desktop.
Save jameshwang/5335044 to your computer and use it in GitHub Desktop.
Go Resources

Learning Golang

Book

The Way to Go

Types

Elementary default to a zero-value:

  1. int
  2. float
  3. bool
  4. string

Structured (or composite) default to nil value:

  1. struct
  2. array
  3. slice
  4. map
  5. channel

Interfaces - Only describe the behavior of a type

Functions

3 types of functions:

  1. Normal functions with an identifier
  2. Anonymous or lambda functions
  3. Methods
Arrays

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!

Websites

  1. I think structs are the objects/classes of Go
  2. Methods are like prototypes in javascript. They are defined outside of the struct
  3. 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
Go Routines
  1. you call a go routine and they will start work asynchronously (like javascript does naturally)
  2. 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.

Other Quotes:

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. 

link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment