Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Go Functions
Search
Nathan Youngman
June 13, 2015
Programming
110
0
Share
Go Functions
Functions in Go, including first-class functions and closures.
Nathan Youngman
June 13, 2015
More Decks by Nathan Youngman
See All by Nathan Youngman
The Healthy Programmer
nathany
2
120
Go and Node.js: a comparison
nathany
1
250
Diet Hacks
nathany
2
390
Go 1.6 and HTTP/2
nathany
3
160
Upgrading Rails Redux
nathany
1
100
GopherCon recap
nathany
0
190
Go Arrays & Slices
nathany
0
170
Go Types
nathany
2
140
Go Packages
nathany
2
570
Other Decks in Programming
See All in Programming
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
340
AIエージェントで業務改善してみた
taku271
0
540
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.6k
実用!Hono RPC2026
yodaka
2
250
ハーネスエンジニアリングとは?
kinopeee
12
5.9k
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
450
SREに優しいTerraform構成 modulesとstateの組み方
hiyanger
2
150
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
220
NakouPAY説明用
annouim0
0
250
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
170
PHP で mp3 プレイヤーを実装しよう
m3m0r7
PRO
0
290
forteeの改修から振り返るPHPerKaigi 2026
muno92
PRO
3
290
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
200
Typedesign – Prime Four
hannesfritz
42
3k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
150
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
220
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Crafting Experiences
bethany
1
120
Scaling GitHub
holman
464
140k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
530
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
340
Deep Space Network (abreviated)
tonyrice
0
120
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
320
Transcript
Go Functions Startup Edmonton Hack Day Saturday, June 13, 2015
Today ✴ Functions ✴ Parameters & arguments ✴ Variable scope
✴ Pass by value ✴ Return results ✴ Named results ✴ First-class functions ✴ Closures
play.golang.org
Last time var path string name type keyword package main
import "fmt" func main() { var name string name = "Kim" fmt.Println("Hello", name) }
Function declaration func playSong(path string) { // engage in funky
music } keyword function name name type first parameter
Calling a function playSong("/Music/Andy Hunter/Exodus/Go.mp3") function name first argument var
song = "/Music/Andy Hunter/Exodus/Go.mp3" playSong(song)
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
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
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
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") }
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 }
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 }
Result name and type package main import "fmt"
func playSong(path string) (err error) { err = fmt.Errorf("Unable to read: %s", path) return err }
Just the result type package main import "fmt"
func playSong(path string) error { var err = fmt.Errorf("Unable to read: %s", path) return err }
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") }
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") }
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
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
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
Nathan Youngman @nathany speakerdeck.com/nathany