
Introducing Go: Build Reliable, Scalable Programs

Because the length of an array is part of its type name, working with arrays can be a little cumbersome. Adding or removing elements as we did here requires also changing the length inside the brackets. Because of this and other limitations, you rarely see arrays used directly in Go code. Instead, you will usually use a slice, which is a type built
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Channel Direction We can specify a direction on a channel type, thus restricting it to either sending or receiving. For example, pinger’s function signature can be changed to this: func pinger(c chan<- string) Now pinger is only allowed to send to c. Attempting to receive from c will result in a compile-time error. Similarly, we can change printer
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
We can also delete items from a map using the built-in delete function: delete(x, 1)
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
we created a function that called the panic function to cause a runtime error. We can handle a runtime panic with the built-in recover function. recover stops the panic and returns the value that was passed to the call to panic.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Because creating a new variable with a starting value is so common, Go also supports a shorter statement: x := "Hello, World" Notice the : before the = and that no type was specified. The type is not necessary because the Go compiler is able to infer the type based on the literal value you assign the variable (because you are assigning a string
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Variables in Go are created by first using the var keyword, then specifying the variable name (x) and the type (string), and finally, assigning a value to the variable (Hello, World). Assigning a value is optional, so we could use two statements, like this: package main import "fmt" func main() { var x string x = "Hello, World" fmt.Println(x) }
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
A goroutine is a function that is capable of running concurrently with other functions. To create a goroutine, we use the keyword go followed by a function invocation:
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
The Sort function in sort takes a sort.Interface and sorts it. The sort.Interface requires three methods: Len, Less, and Swap.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
The * and & operators In Go, a pointer is represented using an asterisk (*) followed by the type of the stored value. In the zero function, xPtr is a pointer to an int. An asterisk is also used to dereference pointer variables. Dereferencing a pointer gives us access to the value the pointer points to. When we write *xPtr = 0, we are saying “store
... See more