
Introducing Go: Build Reliable, Scalable Programs

Normally, when we invoke a function, our program will execute all the statements in a function and then return to the next line following the invocation. With a goroutine, we return immediately to the next line and don’t wait for the function to complete.
Caleb Doxsey • 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
new Another way to get a pointer is to use the built-in new function: func one(xPtr *int) { *xPtr = 1 } func main() { xPtr := new(int) one(xPtr) fmt.Println(*xPtr) // x is 1 } new takes a type as an argument, allocates enough memory to fit a value of that type, and returns a pointer to it. In some programming languages, there is a significant diffe
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
defer is often used when resources need to be freed in some way. For example, when we open a file, we need to make sure to close it later. With defer: f, _ := os.Open(filename) defer f.Close() This has three advantages: It keeps our Close call near our Open call so it’s easier to understand. If our function had multiple return statements (perhaps o
... See moreCaleb 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
Variadic Functions There is a special form available for the last parameter in a Go function: func add(args ...int) int { total := 0 for _, v := range args { total += v } return total } func main() { fmt.Println(add(1,2,3)) } In this example, add is allowed to be called with multiple integers. This is known as a variadic parameter. By using an elli
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Go is a general-purpose programming language with advanced features and a clean syntax. Because of its wide availability on a variety of platforms, its robust well-documented common library, and its focus on good software engineering principles, Go is a great programming language to learn.
Caleb 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
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.