Go basics
The best way to get introduced to Go is to go through tour.golang.com. We will assume you have done that before starting this book. If you have not gone through it yet, or it has been a while, some syntactic reminders are below.
a := 10
means to assign and allocate a new variable. It is equivalent to writing:
var a int64 = 10
defer
is a magic word that says run this function when this scope closes. It's often used to close file connections when the file is no longer being used.
go test()
is a Go function. This allows quick creation of a new thread and the function to be run concurrently.
Functions, types and variables that start with capital letters are public and exported from a package. They are visible in documentation that is generated. Their documentation is autogenerated from comments in the line above the function or type that starts with their name. Comments are prefixed with //
.
// Export is a function that exports things. This is a documentation comment on
// a function. It is good style to put documentation on all public things in Go.
func Export() error {
return nil
}
go fmt
formats Go files. Go has a published style guide that is followed by most folks in the community. You can read about it at Effective Go and Go Code Review Comments. Jaana Dogan has a popular article on the topic Style guideline for Go packages. You can use Go's golint tool to tell you when you are not following the most common style suggestions. go vet
also tries to catch common mistakes.
This page is a preview of Reliable Webservers with Go