SlideShare a Scribd company logo
An Ultimate GopherLabs
Hands-on Labs
funcs, func expressions, closure, returning funcs, recursion, the stack
• Docker Community Leader , Bangalore
• Author :- lightweight Kubernetes with k3s
with packt Publication
• Gopherlabs – 500+ tutorials
• Okteto – Kubernetes For Developer , Bangalore
Meetup Organizer
Who Am I?
@BiradarSangam
Sangam Biradar
EngineITops.com
● function
● functions in go are types
○ functions behave as types in go
○ use like any other type
■ declare them as variables
■ pass functions around just as you'd pass types around
■ pass functions just like any other argument / parameter
● pass them into functions as arguments
● return them from functions
■ declare functions inside other functions
○ similar to JavaScript
● func main – the entry to your program
● parameter and arguments
https://play.golang.org/p/gwfVXFxKi21
parameter
argument
● calling func
https://play.golang.org/p/V_yybIPjmiN
calling a function
You need the ()
● two params
https://play.golang.org/p/aR7fExQ1Ri6
parameters
● func return
https://play.golang.org/p/K9nHsqXnWWY
● named return
https://play.golang.org/p/K_vKaX2H6-Z
● multiple return
https://play.golang.org/p/0QY1qEt1VFw
● variadic parameters
https://play.golang.org/p/N47Hp8Im16i
● variadic argument
https://play.golang.org/p/AD2xjb7dQC5
https://play.golang.org/p/cJL4Z6ksIRC
parameter name does not have to match argument name
● Exercise
Write a function which takes an integer and returns two values:
● the integer divided by 2
● whether or not the integer is even (true, false)
For example
● half(1) should return (0, false)
● half(2) should return (1, true).
● func expression - setting a variable equal to a function
this is not a func expression
this is our code before using a func expression
● func expression
func expression
setting a variable equal to a func the scope of greeting is func main()
this is not a func expression
this is a func expression
https://play.golang.org/p/ux-KmWuN0Lf
https://play.golang.org/p/KQrnwYAZHil
interesting to look at greeting’s type
● func expression
another func expression
setting a variable equal to a func
https://play.golang.org/p/nulzHEt-d96
● Closure
my definition: “one thing enclosing another thing”
● closure
https://play.golang.org/p/p3kWmzBrLcx
closure
func main encloses func increment
closure helps us limit the scope of variables that are used by
multiple functions
without closure, for two or more funcs to have access to the
same variable, that variable would need to be package scope
func main is enclosing increment; increment is enclosing x
● not closure
https://play.golang.org/p/H5tfU6SUocI
not using closure
closure helps us limit the scope of variables that are used
by multiple functions
without closure, for two or more funcs to have access to
the same variable, that variable would need to be package
scope
● returning a func
https://play.golang.org/p/9t8CtlKWHwS
closure
closure helps us limit the scope of variables that are used
by multiple functions
without closure, for two or more funcs to have access to
the same variable, that variable would need to be package
scope
another func expression
setting a variable equal to a func
returning a func
(not part of func expression)
a func is returned
https://play.golang.org/p/ZiU7XUDU7Gh
● callback - passing a func as an argument
func visit takes two arguments
a slice of ints
another func
the callback
pass in the
slice of ints
https://play.golang.org/p/yyV7chHfFR6
●
passin
a
func
the
callback
func visit takes two arguments
a slice of ints
another func
the callback
pass in the
slice of ints
pass in a func
the callback
● How its work …
the func passed as an argument
(the callback) is assigned to the
parameter “callback”
and then gets used
wikipedia’s description
● another callback
https://play.golang.org/p/Pc1popwIdrI
● another callback
“If you’ve done functional programming like Lisp or Haskell, this way of dealing with functions is super common; it’s an
approach to development; you get used to passing functions around. Go allows you to do that [passing functions around]
but it’s not the most common way of writing code. The more normal way you’d write code [for something like the code
above] would just be a simple for loop. For loops are easy to understand.”
~ Caleb Doxsey
● recursion - a func that can call itself
● factorial(4)
○ returns: 4 * factorial(3)
● factorial(3)
○ returns: 3 * factorial(2)
● factorial(2)
○ returns: 2 * factorial(1)
● factorial(1)
○ returns: 1 * factorial(0)
● factorial(0)
○ returns: 1
----------------------------------------------------
returns: 4 * 3 * 2 * 1 * 1
----------------------------------------------------
The End Result:
● 4 * 3 * 2 * 1
https://play.golang.org/p/ZG03lKqaipD
● recursion
● You can always use loops to solve any problem that
can be solved with recursion.
● Loops are more performant than recursion.
This is called the base case
● defer - run this at the last possible moment
https://play.golang.org/p/QHsLIbenzxa
●
● defer doc
● Defer statements
the stack
http://www.golang-book.com/books/intro/7
Review
● func main() {}
● calling a function
● greeting()
● parameters vs arguments
○ two params
○ variadic
■ …params
■ args...
● returns
○ named returns
○ multiple returns
● variable shadowing
● func expression
○ setting a variable equal to a function
○ greeting := func(){<code here>}
■ greeting’s type is func
● closure
○ one thing enclosing another
○ helps us limit scope of variables
● returning a func
○ functional programming
● callback
○ passing a func as an argument
● recursion
● defer
● the stack
○ the order in which functions are
called
Thanks!
Any questions?
@sangambiradar@BiradarSangamSangam Biradar

More Related Content

PDF
Types - slice, map, new, make, struct - Gopherlabs
PDF
kikstart journey of Golang with Hello world - Gopherlabs
PDF
Decision making - for loop , nested loop ,if-else statements , switch in goph...
PPTX
Golang basics for Java developers - Part 1
PPT
Collaboration With Git and GitHub
PDF
Latex with git
PPTX
JavaScript code academy - introduction
ODP
GulpJs - An Introduction
Types - slice, map, new, make, struct - Gopherlabs
kikstart journey of Golang with Hello world - Gopherlabs
Decision making - for loop , nested loop ,if-else statements , switch in goph...
Golang basics for Java developers - Part 1
Collaboration With Git and GitHub
Latex with git
JavaScript code academy - introduction
GulpJs - An Introduction

What's hot (20)

PPTX
Wonders of Golang
PDF
Dockerize Spago Self Contained ML & NLP Library & Deploy on Okteto Cloud Usin...
PDF
Git introduction
PDF
10 reasons to be excited about go
PDF
Vagrant, Ansible and Docker - How they fit together for productive flexible d...
PPT
Come With Golang
PPTX
Introduction to Gulp
PDF
SciPipe - A light-weight workflow library inspired by flow-based programming
ODP
Gopenflow demo v1 (english)
PPTX
Run C++ as serverless with GCP Cloud Functions
PDF
Dockerfile for rust project
PDF
Improving your workflow with gulp
PDF
Taking containers from development to production
PDF
Docker 101 - From production to development
PPTX
01 - Git vs SVN
ODP
Javascript in Linux Desktop
PDF
A gentle intro to Golang and the Go-universe
PDF
[Quality Meetup #20] Michał Górski - Continuous Deployment w chmurze
PPT
Workshop For pycon13
PDF
Git hooks
Wonders of Golang
Dockerize Spago Self Contained ML & NLP Library & Deploy on Okteto Cloud Usin...
Git introduction
10 reasons to be excited about go
Vagrant, Ansible and Docker - How they fit together for productive flexible d...
Come With Golang
Introduction to Gulp
SciPipe - A light-weight workflow library inspired by flow-based programming
Gopenflow demo v1 (english)
Run C++ as serverless with GCP Cloud Functions
Dockerfile for rust project
Improving your workflow with gulp
Taking containers from development to production
Docker 101 - From production to development
01 - Git vs SVN
Javascript in Linux Desktop
A gentle intro to Golang and the Go-universe
[Quality Meetup #20] Michał Górski - Continuous Deployment w chmurze
Workshop For pycon13
Git hooks
Ad

Similar to funcs, func expressions, closure, returning funcs, recursion, the stack -gopherlabs (20)

PDF
Functional go
PDF
golang_refcard.pdf
PDF
Pointers & functions
PPTX
golang_getting_started.pptx
PPTX
Go Programming Language (Golang)
PDF
Go Lang Tutorial
PDF
Golang and Eco-System Introduction / Overview
PPTX
Should i Go there
PDF
Golang
PPTX
Introduction to golang
PDF
Golang from Scala developer’s perspective
PPTX
The GO Language : From Beginners to Gophers
KEY
Beauty and Power of Go
PDF
Functional Go
PDF
TI1220 Lecture 6: First-class Functions
PDF
Golang workshop
PDF
A quick introduction to go
ODP
Ready to go
PDF
C- language Lecture 4
PDF
Inroduction to golang
Functional go
golang_refcard.pdf
Pointers & functions
golang_getting_started.pptx
Go Programming Language (Golang)
Go Lang Tutorial
Golang and Eco-System Introduction / Overview
Should i Go there
Golang
Introduction to golang
Golang from Scala developer’s perspective
The GO Language : From Beginners to Gophers
Beauty and Power of Go
Functional Go
TI1220 Lecture 6: First-class Functions
Golang workshop
A quick introduction to go
Ready to go
C- language Lecture 4
Inroduction to golang
Ad

More from sangam biradar (16)

PDF
Terrascan - Cloud Native Security Tool
PDF
XCloudLabs- AWS Overview
PDF
Rustlabs Quick Start
PDF
Okteto For Kubernetes Developer :- Container Camp 2020
PDF
Happy Helming With Okteto
PDF
5 cool ways to get started with Cloud Native Development ( with Okteto)
PDF
Docker + Tenserflow + GOlang - Golang singapore Meetup
PPTX
Cloud Native Okteto Cloud
PDF
Google ko: fast Kubernetes microservice development in Go - Sangam Biradar, E...
PDF
welcome to gopherlabs - why go (golang)?
PDF
Rabncher Meetup India , Lightweight Kubernetes Development with K3s, k3os and...
PDF
TensorFlow, Docker & GoLang - All for Image Rekognition Sangam Biradar(Engine...
PDF
Introducing Pico - A Deep Learning Platform using Docker & IoT - Sangam Biradar
PDF
September 7, 2019 Cloud Native and Containerisation (Joint Meetup with Docke...
PPTX
Implementing Lightweight Kubernetes(K3s) on Raspberry Pi Stack - Sangam Biradar
PDF
Docker on IOT - Dockercon19 SFO Recap & Announcements, Bangalore
Terrascan - Cloud Native Security Tool
XCloudLabs- AWS Overview
Rustlabs Quick Start
Okteto For Kubernetes Developer :- Container Camp 2020
Happy Helming With Okteto
5 cool ways to get started with Cloud Native Development ( with Okteto)
Docker + Tenserflow + GOlang - Golang singapore Meetup
Cloud Native Okteto Cloud
Google ko: fast Kubernetes microservice development in Go - Sangam Biradar, E...
welcome to gopherlabs - why go (golang)?
Rabncher Meetup India , Lightweight Kubernetes Development with K3s, k3os and...
TensorFlow, Docker & GoLang - All for Image Rekognition Sangam Biradar(Engine...
Introducing Pico - A Deep Learning Platform using Docker & IoT - Sangam Biradar
September 7, 2019 Cloud Native and Containerisation (Joint Meetup with Docke...
Implementing Lightweight Kubernetes(K3s) on Raspberry Pi Stack - Sangam Biradar
Docker on IOT - Dockercon19 SFO Recap & Announcements, Bangalore

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Machine learning based COVID-19 study performance prediction
PDF
Electronic commerce courselecture one. Pdf
PDF
Approach and Philosophy of On baking technology
PPTX
A Presentation on Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
Advanced methodologies resolving dimensionality complications for autism neur...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Empathic Computing: Creating Shared Understanding
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Machine learning based COVID-19 study performance prediction
Electronic commerce courselecture one. Pdf
Approach and Philosophy of On baking technology
A Presentation on Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Monthly Chronicles - July 2025

funcs, func expressions, closure, returning funcs, recursion, the stack -gopherlabs