• Statically typed • C syntax • Simple • Built to use multiple processors • Tools design for remote packages • Testing built in Language focused on Software engineering Go features
"strconv" ) func main() { i := flag.Int("lang", 0, " a number between 0 and 11") flag.Parse() a := []string{"你好世界", … } fmt.Printf("%s\n", a[*i]) } Hello World
p := &Person{"Steve", 28} // p is a reference to a Person PrintPerson(*p) // pass a Person PrintPerson(&p) // pass a reference to a Person func PrintPerson(p Person) // function ONLY takes a Person func PrintPerson(p *Person) // function ONLY takes a reference to a Person Examples of pointers From https://gist.github.com/josephspurrier/7686b139f29601c3b370
language in which to get greeting.") flag.Parse() g := map[string]string{"Chinese":"你好世界", … } if r, ok := g[*l]; ok { fmt.Printf("%s\n", r) } else { fmt.Printf("The language you selected is not valid.\n") } } Hello World Wait! What the hell is that thing?
$argv[1] = "English"; } if (array_key_exists($argv[1], $greetings)){ echo $greetings[$argv[1]]."\n"; } else { die("The language you selected is not valid.\n"); } Hello World
language in which to get greeting.") flag.Parse() g := map[string]string{"Chinese":"你好世界", … } if r, ok := g[*l]; ok { fmt.Printf("%s\n", r) } else { fmt.Printf("The language you selected is not valid.\n") } } Hello World
language in which to get greeting.") flag.Parse() g := map[string]string{"Chinese":"你好世界", … } if r, ok := g[*l]; ok { fmt.Printf("%s\n", r) } else { fmt.Printf("The language you selected is not valid.\n") } } Hello World
than long. This is especially true for local variables with limited scope. Prefer c to lineCount. Prefer i to sliceIndex. https://github.com/golang/go/wiki/CodeReviewComments#variable-names
while still being descriptive • The further away from which they are used, the longer they can be. • Local variable - r • Package public variable - rate
"你好世界", … } func Greet(language string) (string, error) { if r, ok := greetings[language]; ok { return r, nil } else { return "", errors.New("The language you selected is not valid.") } } Hello World
language in which to get greeting.") flag.Parse() greet, err := helloworld.Greet(*l) if err != nil { log.Fatal(err.Error()) } fmt.Printf("%s\n", greet) } Error Handling
language in which to get greeting.") flag.Parse() greet, err := helloworld.Greet(*l) if err != nil { if err == helloworld.ErrNotFound { listLanguages() return } else { log.Fatal(fmt.Errorf("could not get greeting: %v\n”, err)) } } fmt.Printf("%s\n", greet) } Error Handling
"你好世界", … } var ErrNotFound = errors.New("the language you selected is not valid") func Greet(language string) (string, error) {…} func helper(language string) (int) {…} Hello World
"你好世界", … } var ErrNotFound = errors.New("the language you selected is not valid") func Greet(language string) (string, error) {…} func Langauges() []string Web app - base package
languages package helloworld import "errors" // ErrorNotFound occurs when the input language is not one we have // in our collection var ErrorNotFound = errors.New("the language you selected is not valid") // Greet returns a greeting in the input language. If the languages is not // found, returns ErrorNotFound func Greet(language string) (string, error) {…} // Languages returns a list of the supported languages func Languages() []string {…} Seeing Documentation But Better
template.Template) error { var wg sync.WaitGroup wg.Add(count) for i := 1; i <= count; i++ { go func(i int, wg *sync.WaitGroup) { defer wg.Done() err := writeEntries(entries, outdir+strconv.Itoa(i), t) if err != nil { log.Fatal(err) } }(i, &wg) } wg.Wait() return nil } Use concurrency
color generator • PHP • gcr.io/google_appengine/php:latest • Admin - Kubernetes API Proxy • PHP • gcr.io/google_appengine/php:latest • Game - UI for app • HTML/JS/CSS • gcr.io/google_appengine/php:latest Task 3 Before After API 171 MB Admin 171 MB Game 181 MB Total 523 MB
All Go workspaces have 3 parts • src • bin • pkg • Most everything happens in src • github.com/username/project • Set folder that contains src, bin, pkg to GOPATH • export GOPATH=$HOME/go Go Path