Breanna shows 3 useful resources from the Go website
The general big picture of Go. Breanna recommend the "Effective Go" link.
You can also run go doc <search>
.
Two major differences:
func main
for a main function. This is the entry point for the program.package main import "fmt" func main() { fmt.Printf("Hi! My name is %s. I have lived in %s.", "Dennis", "O'Keeffe") }
Breana uses the reflect
page to find the types that come out.
package main import ( "fmt" "reflect" ) func main() { var x = 4 fmt.Printf(reflect.TypeOf(float64(x) * 5.5)) }
go run main.go # compile and run go install # behaves almost identically to go build , but instead of leaving the executable in the current directory go build # build binary go fmt main.go # format file go list # show packages establish in the code package go list ./... # show all packages in this directory go doc fmt.Println # show go doc for file go get golang.org/x/lint/golint # install Go packages golint # run linter go vet # Go source code and reports suspicious constructs
Breana creates a file utils/math.go
to use as a utility for the main package.
// math.go package utils import "fmt" // Add takes n ints and returns sum func Add(nums ...int) int { total := 0 for _,v := rage nums { total += v } } // packages.go package main import ( "fmt" math "root/path/to/utils" // alias to math ) func calculateImportantData() int { totalValue := math.Add(1,2,3,4) return totalValue } func main() { fmt.Println("packages!") total := calculateImportantData() fmt.Println("Total", total) }
f, err := os.Open(filename) defer f.Close() // run at the end of the block scope panic(err.Error()) // throw error
Defer also in a "last-in-first-out" structure.
Panic is called during a run time error and fatally kill the program. Recover tells Go what to do when that happens.
Recover MUST be paired with defer, which will fire even after a panic.
func recoverFromPanic() { if r := recover(); r } func doThings() { defer recoverFromPanic() for i := 0; i < 5; i++ { fmt.Println(i) if i == 2 { panic("PANIC!") } } }