
Introducing Go: Build Reliable, Scalable Programs

Package names match the folders they fall in. There are ways around this, but it’s a lot easier if you stay within this pattern.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Go has several different types to represent numbers.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Closure It is possible to create functions inside of functions. Let’s move the add function we saw before inside of main: func main() { add := func(x, y int) int { return x + y } fmt.Println(add(1,1)) } add is a local variable that has the type func(int, int) int (a function that takes two ints and returns an int). When you create a local function
... 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 := <- c1:
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Go also provides a shorter syntax for creating arrays: x := [5]float64{ 98, 93, 77, 82, 83 }
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
string is a sequence of characters with a definite length used to represent text. Go strings are made up of individual bytes, usually one for each character (characters from some languages, such as Chinese, are represented by more than one byte).
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
In addition to the indexing operator, Go includes two built-in functions to assist with slices: append and copy. append append adds elements onto the end of a slice. If there’s sufficient capacity in the underlying array, the element is placed after the last element and the length is incremented. However, if there is not sufficient capacity, a new
... See moreCaleb Doxsey • 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
Multiple values are often used to return an error value along with the result (x, err := f()), or a boolean to indicate success (x, ok := f()).