
Introducing Go: Build Reliable, Scalable Programs

List The container/list package implements a doubly linked list. A linked list is a type of data structure that looks like Figure 8-1. Figure 8-1. A linked list Each node of the list contains a value (1, 2, or 3, in this case) and a pointer to the next node. Because this is a doubly linked list, each node will also have pointers to the previous nod
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
The fmt package (shorthand for format) implements formatting for input and output.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
time.After creates a channel, and after the given duration, will send the current time on it (we weren’t interested in the time, so we didn’t store it in a variable).
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Go also has a mechanism for combining interfaces, types, variables, and functions together into a single component known as a package.
Caleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
Input/Output Before we look at files, we need to understand Go’s io package. The io package consists of a few functions, but mostly interfaces used in other packages. The two main interfaces are Reader and Writer. Readers support reading via the Read method. Writers support writing via the Write method. Many functions in Go take Readers or Writers
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
In Go, we can create a TCP server using the net package’s Listen function. Listen takes a network type (in our case, tcp) and an address and port to bind, and returns a net.Listener: type Listener interface { // Accept waits for and returns the next connection to the listener. Accept() (c Conn, err error) // Close closes the listener. // Any blocke
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
The first line says this: package main This is known as a package declaration, and every Go program must start with it. Packages are Go’s way of organizing and reusing code. There are two types of Go programs: executables and libraries. Executable applications are the kinds of programs that we can run directly from the terminal (on Windows, they en
... See moreCaleb Doxsey • Introducing Go: Build Reliable, Scalable Programs
A struct is a type that contains named fields. For example, we could represent a circle like this: type Circle struct { x float64 y float64 r float64 } The type keyword introduces a new type. It’s followed by the name of the type (Circle), the keyword struct to indicate that we are defining a struct type, and a list of fields inside of curly braces
... 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 more