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
96
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
100
Go and Node.js: a comparison
nathany
1
200
Diet Hacks
nathany
2
360
Go 1.6 and HTTP/2
nathany
3
130
Upgrading Rails Redux
nathany
1
88
GopherCon recap
nathany
0
170
Go Arrays & Slices
nathany
0
140
Go Types
nathany
2
130
Go Packages
nathany
2
540
Other Decks in Programming
See All in Programming
GoのGenericsによるslice操作との付き合い方
syumai
2
670
「ElixirでIoT!!」のこれまでとこれから
takasehideki
0
370
Prism.parseで 300本以上あるエンドポイントに 接続できる権限の一覧表を作ってみた
hatsu38
1
110
CursorはMCPを使った方が良いぞ
taigakono
0
120
セキュリティマネジャー廃止とクラウドネイティブ型サンドボックス活用
kazumura
1
190
The Evolution of Enterprise Java with Jakarta EE 11 and Beyond
ivargrimstad
1
820
統一感のある Go コードを生成 AI の力で手にいれる
otakakot
0
3k
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
550
SODA - FACT BOOK
sodainc
1
1.1k
今ならAmazon ECSのサービス間通信をどう選ぶか / Selection of ECS Interservice Communication 2025
tkikuc
11
2.6k
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
2
540
レガシーシステムの機能調査・開発におけるAI利活用
takuya_ohtonari
0
610
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
Facilitating Awesome Meetings
lara
54
6.4k
How to Ace a Technical Interview
jacobian
277
23k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Designing for Performance
lara
609
69k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
How to train your dragon (web standard)
notwaldorf
92
6.1k
Site-Speed That Sticks
csswizardry
10
650
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
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