
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
RPC The net/rpc (remote procedure call) and net/rpc/jsonrpc packages provide an easy way to expose methods so they can be invoked over a network (rather than just in the program running them): package main import ( "fmt" "net" "net/rpc" ) type Server struct {} func (this *Server) Negate(i int64, reply *int64) error { *
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Go has a way of making these accidental similarities explicit through a type known as an interface. Here is an example of a Shape interface: type Shape interface { area() float64 } Like a struct, an interface is created using the type keyword, followed by a name and the keyword interface. But instead of defining fields, we define a method set. A me
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Go has two floating-point types: float32 and float64 (also often referred to as single precision and double precision, respectively). It also has two additional types for representing complex numbers (numbers with imaginary parts): complex64 and complex128. Generally, we should stick with float64 when working with floating-point numbers.
Caleb Doxsey • 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
Go is a statically typed programming language. This means that variables always have a specific type and that type cannot change.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
we need to convert len(x) into a float64: fmt.Println(total / float64(len(x))) This is an example of a type conversion. In general, to convert between types, you use the type name like a function.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
A slice is a segment of an array. Like arrays, slices are indexable and have a length. Unlike arrays, this length is allowed to change.