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
0
90
Go Functions
Functions in Go, including first-class functions and closures.
Nathan Youngman
June 13, 2015
Tweet
Share
More Decks by Nathan Youngman
See All by Nathan Youngman
The Healthy Programmer
nathany
2
93
Go and Node.js: a comparison
nathany
1
170
Diet Hacks
nathany
2
340
Go 1.6 and HTTP/2
nathany
3
110
Upgrading Rails Redux
nathany
1
84
GopherCon recap
nathany
0
150
Go Arrays & Slices
nathany
0
120
Go Types
nathany
2
120
Go Packages
nathany
2
520
Other Decks in Programming
See All in Programming
月刊 競技プログラミングをお仕事に役立てるには
terryu16
1
1.2k
Findy Team+ Awardを受賞したかった!ベストプラクティス応募内容をふりかえり、開発生産性向上もふりかえる / Findy Team Plus Award BestPractice and DPE Retrospective 2024
honyanya
0
140
Azure AI Foundryのご紹介
qt_luigi
1
210
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
180
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
快速入門可觀測性
blueswen
0
500
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
.NETでOBS Studio操作してみたけど…… / Operating OBS Studio by .NET
skasweb
0
120
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.9k
為你自己學 Python
eddie
0
520
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
240
QA環境で誰でも自由自在に現在時刻を操って検証できるようにした話
kalibora
1
140
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
210
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
GitHub's CSS Performance
jonrohan
1030
460k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Site-Speed That Sticks
csswizardry
3
270
Mobile First: as difficult as doing things right
swwweet
222
9k
YesSQL, Process and Tooling at Scale
rocio
170
14k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
GraphQLとの向き合い方2022年版
quramy
44
13k
The Invisible Side of Design
smashingmag
299
50k
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