
Introducing Go: Build Reliable, Scalable Programs

The go test command will look for any tests in any of the files in the current folder and run them. Tests are identified by starting a function with the word Test and taking one argument of type *testing.T.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Closure and recursion are powerful programming techniques that form the basis of a paradigm known as functional programming. Most people will find functional programming more difficult to understand than an approach based on for loops, if statements, variables, and simple functions.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Buffered Channels It’s also possible to pass a second parameter to the make function when creating a channel: c := make(chan int, 1) This creates a buffered channel with a capacity of 1. Normally, channels are synchronous; both sides of the channel will wait until the other side is ready. A buffered channel is asynchronous; sending or receiving a m
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
String literals can be created using double quotes "Hello, World" or backticks Hello, World . The difference between these is that double-quoted strings cannot contain newlines and they allow special escape sequences. For example, \n gets replaced with a newline and \t gets replaced with a tab character.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Embedded Types A struct’s fields usually represent the has-a relationship (e.g., a Circle has a radius). The following snippet shows an example of a person struct: type Person struct { Name string } func (p *Person) Talk() { fmt.Println("Hi, my name is", p.Name) } Now suppose we wanted to create a new Android struct. We could do this: typ
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
If you want to create a slice, you should use the built-in make function: x := make([]float64, 5) This creates a slice that is associated with an underlying float64 array of length 5. Slices are always associated with some array, and although they can never be longer than the array, they can be smaller. The make function also allows a third paramet
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Select Go has a special statement called select that works like a switch but for channels: func main() { c1 := make(chan string) c2 := make(chan string) go func() { for { c1 <- "from 1" time.Sleep(time.Second * 2) } }() go func() { for { c2 <- "from 2" time.Sleep(time.Second * 3) } }() go func() { for { select { case msg1
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
A channel type is represented with the keyword chan followed by the type of the things that are passed on the channel (in this case, we are passing strings). The left arrow operator (<-) is used to send and receive messages on the channel. c <- "ping" means send "ping". msg := <- c means receive a message and store it i
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
A map is an unordered collection of key-value pairs (maps are also sometimes called associative arrays, hash tables, or dictionaries). Maps are used to look up a value by its associated key. Here’s an example of a map in Go: var x map[string]int The map type is represented by the keyword map, followed by the key type in brackets and finally the val
... See more