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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Nathan Youngman
June 13, 2015
Programming
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
400
Go 1.6 and HTTP/2
nathany
3
160
Upgrading Rails Redux
nathany
1
110
GopherCon recap
nathany
0
200
Go Arrays & Slices
nathany
0
180
Go Types
nathany
2
140
Go Packages
nathany
2
570
Other Decks in Programming
See All in Programming
ふつうのFeature Flag実践入門
irof
8
4.2k
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
920
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
270
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
170
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.4k
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
120
Claspは野良GASの夢をみるか
takter00
0
210
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
180
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.5k
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
230
スマートグラスで並列バイブコーディング
hyshu
0
260
AI 輔助遺留系統現代化的經驗分享
jame2408
1
970
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
160
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
400
Statistics for Hackers
jakevdp
799
230k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
470
Everyday Curiosity
cassininazir
0
240
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
The Curse of the Amulet
leimatthew05
2
13k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
150
Odyssey Design
rkendrick25
PRO
2
700
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
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