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
94
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
97
Go and Node.js: a comparison
nathany
1
180
Diet Hacks
nathany
2
350
Go 1.6 and HTTP/2
nathany
3
120
Upgrading Rails Redux
nathany
1
86
GopherCon recap
nathany
0
160
Go Arrays & Slices
nathany
0
130
Go Types
nathany
2
120
Go Packages
nathany
2
530
Other Decks in Programming
See All in Programming
Kubernetesで実現できるPlatform Engineering の現在地
nwiizo
2
1.7k
生産性アップのためのAI個人活用
kunoyasu
0
650
PHPUnit 高速化テクニック / PHPUnit Speedup Techniques
pinkumohikan
1
1.2k
DomainException と Result 型で作る型安全なエラーハンドリング
karszawa
0
620
アーキテクトと美学 / Architecture and Aesthetics
nrslib
12
3.1k
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.3k
PHPのガベージコレクションを深掘りしよう
rinchoku
0
250
The Weight of Data: Rethinking Cloud-Native Systems for the Age of AI
hollycummins
0
110
PHPでお金を扱う時、終わりのない 謎の1円調査の旅にでなくて済む方法
nakka
3
1.4k
データベースエンジニアの仕事を楽にする。PgAssistantの紹介
nnaka2992
9
4.3k
Go1.24で testing.B.Loopが爆誕
kuro_kurorrr
0
170
SLI/SLOの設定を進めるその前に アラート品質の改善に取り組んだ話
tanden
2
740
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
31
4.8k
GitHub's CSS Performance
jonrohan
1030
460k
RailsConf 2023
tenderlove
29
1k
Mobile First: as difficult as doing things right
swwweet
223
9.5k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
500
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.1k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
How to Ace a Technical Interview
jacobian
276
23k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Testing 201, or: Great Expectations
jmmastey
42
7.4k
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