Slide 1

Slide 1 text

Go Functions Startup Edmonton Hack Day Saturday, June 13, 2015

Slide 2

Slide 2 text

Today ✴ Functions ✴ Parameters & arguments ✴ Variable scope ✴ Pass by value ✴ Return results ✴ Named results ✴ First-class functions ✴ Closures

Slide 3

Slide 3 text

play.golang.org

Slide 4

Slide 4 text

Last time var path string name type keyword package main
 
 import "fmt"
 
 func main() {
 var name string
 name = "Kim"
 fmt.Println("Hello", name)
 }

Slide 5

Slide 5 text

Function declaration func playSong(path string) {
 // engage in funky music
 } keyword function name name type first parameter

Slide 6

Slide 6 text

Calling a function playSong("/Music/Andy Hunter/Exodus/Go.mp3") function name first argument var song = "/Music/Andy Hunter/Exodus/Go.mp3"
 playSong(song)

Slide 7

Slide 7 text

Variable scope package main
 
 import (
 "fmt"
 "path/filepath"
 )
 
 func playSong(path string) {
 var song = filepath.Base(path)
 fmt.Println("Playing song:", song)
 }
 
 func main() {
 var song = "/Music/Andy Hunter/Exodus/Go.mp3"
 playSong(song)
 } play.golang.org/p/CcjBwYznZo

Slide 8

Slide 8 text

Pass by value package main
 
 import (
 "fmt"
 "path/filepath"
 )
 
 func playSong(path string) {
 path = filepath.Clean(path)
 fmt.Println("Playing music:", path)
 }
 
 func main() {
 var path = "/../Music//Andy Hunter/Exodus/Go.mp3"
 playSong(path)
 fmt.Println("Path in main:", path)
 } play.golang.org/p/cYKdCm10Sv

Slide 9

Slide 9 text

Results func readID3(path string) (title string, year int) {
 // return metadata
 } keyword function name name type parameter name type first result name type second result

Slide 10

Slide 10 text

Results in practice package main
 
 func readID3(path string) (title string, year int) {
 return "One Motion", 2012
 }
 
 func main() {
 var title, year = readID3(“One Motion.mp3")
 }

Slide 11

Slide 11 text

Results of the same type func readID3(path string) (title, artist, album string, year int, track uint8) {
 return "One Motion", "Andy Hunter", "Glow", 2012, 3
 }

Slide 12

Slide 12 text

Bare return package main
 
 func readID3(path string) (title, artist, album string, year int, track uint8) {
 title = "One Motion"
 artist = "Andy Hunter"
 album = "Glow"
 year = 2012
 track = 3
 return
 }


Slide 13

Slide 13 text

Result name and type package main
 
 import "fmt"
 
 func playSong(path string) (err error) {
 err = fmt.Errorf("Unable to read: %s", path)
 return err
 }

Slide 14

Slide 14 text

Just the result type package main
 
 import "fmt"
 
 func playSong(path string) error {
 var err = fmt.Errorf("Unable to read: %s", path)
 return err
 }

Slide 15

Slide 15 text

Announcer package main
 
 import "fmt"
 
 func announce(title, artist string) {
 fmt.Print("And now for the smooth sound of ")
 fmt.Print(artist, " with ", title, ".")
 }
 
 func main() {
 announce("One Motion", "Andy Hunter")
 }


Slide 16

Slide 16 text

Say (OS X) package main
 
 import "os/exec"
 
 func say(text string) {
 var cmd = exec.Command("say", "-v", "Cellos", text)
 cmd.Run()
 }
 
 func main() {
 say("I like pizza")
 }

Slide 17

Slide 17 text

First-class functions package main
 
 import (
 "fmt"
 "os/exec"
 )
 
 func display(text string) {
 fmt.Println(text)
 }
 
 func say(text string) {
 var cmd = exec.Command("say", "-v", "Cellos", text)
 cmd.Run()
 }
 
 func main() {
 var f func(text string)
 f = display
 f("Hello World")
 f = say
 f("Hello World")
 }
 play.golang.org/p/QW2AERS_Ap

Slide 18

Slide 18 text

Higher-order functions a function parameter //... func announce(title, artist string, speak func(text string)) {
 speak("And now for the smooth sound of " + artist + " with " + title + ".")
 }
 
 func display(text string) {
 fmt.Println(text)
 }
 
 func say(text string) {
 var cmd = exec.Command("say", "-v", "Cellos", text)
 cmd.Run()
 }
 
 func main() {
 announce("One Motion", "Andy Hunter", say)
 announce("One Motion", "Andy Hunter", display)
 } play.golang.org/p/T8yTum1wcR

Slide 19

Slide 19 text

Higher-order functions and closures a function result package main
 
 import "os/exec"
 
 func sayGen(voice string) func(text string) {
 return func(text string) {
 var cmd = exec.Command("say", "-v", voice, text)
 cmd.Run()
 }
 }
 
 func main() {
 var say = sayGen("Cellos")
 say("Hello World")
 
 var organ = sayGen("Pipe Organ")
 organ("This sounds like a pipe organ")
 } play.golang.org/p/Wf9f4Kh12O

Slide 20

Slide 20 text

Nathan Youngman @nathany speakerdeck.com/nathany