SlideShare a Scribd company logo
Development with Go
- Manjitsing K. Valvi
Pointers
● A variable is a piece of storage containing a value (Variables : Addressable values)
● A pointer value is the address of a variable - the location at which a value is stored
● Not every value has an address, but every variable does.
● We can access the value of a variable indirectly using pointers, without knowing/referring to
name of the variable
● Example:
x := 1 // variable of type int
p := &x // p, of type *int, points to x (&x = address of x)
fmt.Println(*p) // *p denotes value pointed by ‘p’ : "1"
*p = 2 // equivalent to x = 2 ; *p can be on LHS of expr
fmt.Println(x) // "2"
● Expressions that denote variables are the only expressions to which the address-of
operator “&” may be applied
new Function
● new(T)
○ creates unnamed variable of Type T
○ Initializes it to zero value of T
○ Returns it address(type *T)
● new is a predeclared function, not a keyword
● Example:
p := new(int) // p, of type *int, points to an unnamed int variable
fmt.Println(*p) // "0"
*p = 2 // sets the unnamed int to 2
fmt.Println(*p) // "2"
Functions
● It is possible to return multiple values from a function
● Multiple return values are specified between ( and )
● It is possible to return named values from a function, it can be considered as being declared
as a variable in the first line of the function
func arithOps(a, b int)(sum, sub int) { // sum and sub are named
sum = a + b // return values
Sub = a - b
return //no explicit return value, sum and sub are returned when
//return is encountered
}
● Blank identifier ‘ _’ can be used if any value is to be discarded from multiple return values
Variadic Functions
● It is a function that accepts a variable number of arguments
● Here the type of the final parameter is preceded by an ellipsis, "...", showing the function
may be called with any number of arguments of this type
● Useful when number of arguments passed to a function are not known
● Built-in Println is best example of variadic functions
func sum(nums ...int) {
fmt.Print(nums, " ")
total := 0
for _, num := range nums {
total += num
}
fmt.Println(total)
}
func main() {
sum(1, 2)
sum(1, 2, 3)
nums := []int{1, 2, 3, 4}
sum(nums...)
}
Nested Functions
● In GO functions can be assigned to variables, passed as arguments to other functions and
returned from other functions => First Class Functions
● GO functions can contain functions
● Functions can literally be defined in functions
● Functions can be Anonymous
● Anonymous functions can have parameters like any other function
func main(){
world := func() string { // Anonymous function
fmt.Print("hello world n")
}
world()
fmt.Print("Type of world %T ", world)
}
User Defined Function Types
● In GO user can create his own function types (like struct)
type add func(a int, b int) int // Creating function type add
// with two int parameters
func main() {
var a add = func(a int, b int) int { // variable of type add
return a + b
}
s := a(5, 6) // calling the function a
fmt.Println("Sum", s)
}
Closures
● is a function that references variables outside of its scope
● can outlive the scope in which it was created
● a special case of anonymous functions
● every closure is bound to its own surrounding variable
func main() {
a := 5
func() {
fmt.Println("a =", a) // Anonymous function accessing ‘a’
// variable outside its body
}() // This function is a closure
}
Defer
● defer is useful when one wants to ensure about function getting executed even in case of
failure of the program e.g closing the files, releasing the resources etc
● defer is also helpful in debugging OR in error handling mechanism of GO
func fun(a int) {
fmt.Println("a =",a)
}
func main() {
a := 5
defer fun(a) // deferred function call
fmt.Println("a =", a) // Anonymous function accessing ‘a’
}
// Output is
// a=6 a=5
// Since the call the function fun() is deferred, it is having the
//value of a as 5
Defer
● Syntax : defer func_call( )
● A function or method call can be prefixed with keyword defer
● Such function and argument expressions are evaluated when the statement is
● executed, but the actual call is deferred until the function containing the defer statement
has finished
● No matter whether the function that contains defer statement ends normally or after
panicking, the deferred function is called
● Any number of calls may be deferred; they are executed in the reverse of the order in
which they were deferred
func main() {
a := 5
defer func(a) // deferred function call
fmt.Println("a =", a) // Anonymous function accessing ‘a’
}
References
● https://golangbot.com/first-class-functions/
● https://livebook.manning.com/book/get-programming-with-go/chapter-14/36

More Related Content

What's hot

Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
Jake Bond
 
Effective PHP. Part 2
Effective PHP. Part 2Effective PHP. Part 2
Effective PHP. Part 2
Vasily Kartashov
 
Effective PHP. Part 4
Effective PHP. Part 4Effective PHP. Part 4
Effective PHP. Part 4
Vasily Kartashov
 
Effective PHP. Part 3
Effective PHP. Part 3Effective PHP. Part 3
Effective PHP. Part 3
Vasily Kartashov
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
Chris Farrell
 
Storage class
Storage classStorage class
Storage class
Joy Forerver
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
Reddhi Basu
 
Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)
Nikos Kalogridis
 
Effective PHP. Part 6
Effective PHP. Part 6Effective PHP. Part 6
Effective PHP. Part 6
Vasily Kartashov
 
Function
FunctionFunction
Function
jasscheema
 
What is storage class
What is storage classWhat is storage class
What is storage class
Isha Aggarwal
 
Functions
FunctionsFunctions
Functions
preetikapri1
 
storage class
storage classstorage class
storage class
student
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 Developers
Jayaram Sankaranarayanan
 
STORAGE CLASSES
STORAGE CLASSESSTORAGE CLASSES
STORAGE CLASSES
sathish sak
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
SURBHI SAROHA
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
NabeelaNousheen
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
imtiazalijoono
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
SURBHI SAROHA
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 

What's hot (20)

Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Effective PHP. Part 2
Effective PHP. Part 2Effective PHP. Part 2
Effective PHP. Part 2
 
Effective PHP. Part 4
Effective PHP. Part 4Effective PHP. Part 4
Effective PHP. Part 4
 
Effective PHP. Part 3
Effective PHP. Part 3Effective PHP. Part 3
Effective PHP. Part 3
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3
 
Storage class
Storage classStorage class
Storage class
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)
 
Effective PHP. Part 6
Effective PHP. Part 6Effective PHP. Part 6
Effective PHP. Part 6
 
Function
FunctionFunction
Function
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Functions
FunctionsFunctions
Functions
 
storage class
storage classstorage class
storage class
 
Functional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 DevelopersFunctional Programming 101 for Java 7 Developers
Functional Programming 101 for Java 7 Developers
 
STORAGE CLASSES
STORAGE CLASSESSTORAGE CLASSES
STORAGE CLASSES
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variable
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 

Similar to Pointers & functions

Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
 
uom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptxuom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptx
ChetanChauhan203001
 
Intro to python programming (basics) in easy language
Intro to python programming (basics) in easy languageIntro to python programming (basics) in easy language
Intro to python programming (basics) in easy language
MuhammadShahbaz36976
 
uom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptxuom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptx
Prabha Karan
 
What is Paython.pptx
What is Paython.pptxWhat is Paython.pptx
What is Paython.pptx
Parag Soni
 
what-is-python-presentation.pptx
what-is-python-presentation.pptxwhat-is-python-presentation.pptx
what-is-python-presentation.pptx
Vijay Krishna
 
python-presentation.pptx
python-presentation.pptxpython-presentation.pptx
python-presentation.pptx
Vijay Krishna
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loops
thirumurugan133
 
Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
Function
FunctionFunction
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
RITHIKA R S
 
Introduction of python Introduction of python Introduction of python
Introduction of python Introduction of python Introduction of pythonIntroduction of python Introduction of python Introduction of python
Introduction of python Introduction of python Introduction of python
GandaraEyao
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
SulekhJangra
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Function in c
Function in cFunction in c
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
Markus Schneider
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser823678
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser2076d9
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
HNDE Labuduwa Galle
 

Similar to Pointers & functions (20)

Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
 
uom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptxuom-2552-what-is-python-presentation.pptx
uom-2552-what-is-python-presentation.pptx
 
Intro to python programming (basics) in easy language
Intro to python programming (basics) in easy languageIntro to python programming (basics) in easy language
Intro to python programming (basics) in easy language
 
uom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptxuom-2552-what-is-python-presentation (1).pptx
uom-2552-what-is-python-presentation (1).pptx
 
What is Paython.pptx
What is Paython.pptxWhat is Paython.pptx
What is Paython.pptx
 
what-is-python-presentation.pptx
what-is-python-presentation.pptxwhat-is-python-presentation.pptx
what-is-python-presentation.pptx
 
python-presentation.pptx
python-presentation.pptxpython-presentation.pptx
python-presentation.pptx
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loops
 
Scala functions
Scala functionsScala functions
Scala functions
 
Function
FunctionFunction
Function
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 
Introduction of python Introduction of python Introduction of python
Introduction of python Introduction of python Introduction of pythonIntroduction of python Introduction of python Introduction of python
Introduction of python Introduction of python Introduction of python
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
Function in c
Function in cFunction in c
Function in c
 
Function in c
Function in cFunction in c
Function in c
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 

More from Manjitsing Valvi

Basic types
Basic typesBasic types
Basic types
Manjitsing Valvi
 
Basic constructs ii
Basic constructs  iiBasic constructs  ii
Basic constructs ii
Manjitsing Valvi
 
Features of go
Features of goFeatures of go
Features of go
Manjitsing Valvi
 
Error handling
Error handlingError handling
Error handling
Manjitsing Valvi
 
Operators
OperatorsOperators
Operators
Manjitsing Valvi
 
Methods
MethodsMethods
Digital marketing marketing strategies for digital world
Digital marketing  marketing strategies for digital worldDigital marketing  marketing strategies for digital world
Digital marketing marketing strategies for digital world
Manjitsing Valvi
 
Digital marketing channels
Digital marketing channelsDigital marketing channels
Digital marketing channels
Manjitsing Valvi
 
Digital marketing techniques
Digital marketing techniquesDigital marketing techniques
Digital marketing techniques
Manjitsing Valvi
 
Social media marketing & managing cybersocial campaign
Social media marketing & managing cybersocial campaignSocial media marketing & managing cybersocial campaign
Social media marketing & managing cybersocial campaign
Manjitsing Valvi
 
Creating marketing effective online store
Creating marketing effective online storeCreating marketing effective online store
Creating marketing effective online store
Manjitsing Valvi
 
Social media marketing tech tools and optimization for search engines
Social media marketing   tech tools and optimization for search enginesSocial media marketing   tech tools and optimization for search engines
Social media marketing tech tools and optimization for search engines
Manjitsing Valvi
 
Digital marketing managing cybersocial campaign
Digital marketing managing cybersocial campaignDigital marketing managing cybersocial campaign
Digital marketing managing cybersocial campaign
Manjitsing Valvi
 
Social media marketing
Social media marketingSocial media marketing
Social media marketing
Manjitsing Valvi
 

More from Manjitsing Valvi (14)

Basic types
Basic typesBasic types
Basic types
 
Basic constructs ii
Basic constructs  iiBasic constructs  ii
Basic constructs ii
 
Features of go
Features of goFeatures of go
Features of go
 
Error handling
Error handlingError handling
Error handling
 
Operators
OperatorsOperators
Operators
 
Methods
MethodsMethods
Methods
 
Digital marketing marketing strategies for digital world
Digital marketing  marketing strategies for digital worldDigital marketing  marketing strategies for digital world
Digital marketing marketing strategies for digital world
 
Digital marketing channels
Digital marketing channelsDigital marketing channels
Digital marketing channels
 
Digital marketing techniques
Digital marketing techniquesDigital marketing techniques
Digital marketing techniques
 
Social media marketing & managing cybersocial campaign
Social media marketing & managing cybersocial campaignSocial media marketing & managing cybersocial campaign
Social media marketing & managing cybersocial campaign
 
Creating marketing effective online store
Creating marketing effective online storeCreating marketing effective online store
Creating marketing effective online store
 
Social media marketing tech tools and optimization for search engines
Social media marketing   tech tools and optimization for search enginesSocial media marketing   tech tools and optimization for search engines
Social media marketing tech tools and optimization for search engines
 
Digital marketing managing cybersocial campaign
Digital marketing managing cybersocial campaignDigital marketing managing cybersocial campaign
Digital marketing managing cybersocial campaign
 
Social media marketing
Social media marketingSocial media marketing
Social media marketing
 

Recently uploaded

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
Madan Karki
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
Manufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptxManufacturing Process of molasses based distillery ppt.pptx
Manufacturing Process of molasses based distillery ppt.pptx
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 

Pointers & functions

  • 1. Development with Go - Manjitsing K. Valvi
  • 2. Pointers ● A variable is a piece of storage containing a value (Variables : Addressable values) ● A pointer value is the address of a variable - the location at which a value is stored ● Not every value has an address, but every variable does. ● We can access the value of a variable indirectly using pointers, without knowing/referring to name of the variable ● Example: x := 1 // variable of type int p := &x // p, of type *int, points to x (&x = address of x) fmt.Println(*p) // *p denotes value pointed by ‘p’ : "1" *p = 2 // equivalent to x = 2 ; *p can be on LHS of expr fmt.Println(x) // "2" ● Expressions that denote variables are the only expressions to which the address-of operator “&” may be applied
  • 3. new Function ● new(T) ○ creates unnamed variable of Type T ○ Initializes it to zero value of T ○ Returns it address(type *T) ● new is a predeclared function, not a keyword ● Example: p := new(int) // p, of type *int, points to an unnamed int variable fmt.Println(*p) // "0" *p = 2 // sets the unnamed int to 2 fmt.Println(*p) // "2"
  • 4. Functions ● It is possible to return multiple values from a function ● Multiple return values are specified between ( and ) ● It is possible to return named values from a function, it can be considered as being declared as a variable in the first line of the function func arithOps(a, b int)(sum, sub int) { // sum and sub are named sum = a + b // return values Sub = a - b return //no explicit return value, sum and sub are returned when //return is encountered } ● Blank identifier ‘ _’ can be used if any value is to be discarded from multiple return values
  • 5. Variadic Functions ● It is a function that accepts a variable number of arguments ● Here the type of the final parameter is preceded by an ellipsis, "...", showing the function may be called with any number of arguments of this type ● Useful when number of arguments passed to a function are not known ● Built-in Println is best example of variadic functions func sum(nums ...int) { fmt.Print(nums, " ") total := 0 for _, num := range nums { total += num } fmt.Println(total) } func main() { sum(1, 2) sum(1, 2, 3) nums := []int{1, 2, 3, 4} sum(nums...) }
  • 6. Nested Functions ● In GO functions can be assigned to variables, passed as arguments to other functions and returned from other functions => First Class Functions ● GO functions can contain functions ● Functions can literally be defined in functions ● Functions can be Anonymous ● Anonymous functions can have parameters like any other function func main(){ world := func() string { // Anonymous function fmt.Print("hello world n") } world() fmt.Print("Type of world %T ", world) }
  • 7. User Defined Function Types ● In GO user can create his own function types (like struct) type add func(a int, b int) int // Creating function type add // with two int parameters func main() { var a add = func(a int, b int) int { // variable of type add return a + b } s := a(5, 6) // calling the function a fmt.Println("Sum", s) }
  • 8. Closures ● is a function that references variables outside of its scope ● can outlive the scope in which it was created ● a special case of anonymous functions ● every closure is bound to its own surrounding variable func main() { a := 5 func() { fmt.Println("a =", a) // Anonymous function accessing ‘a’ // variable outside its body }() // This function is a closure }
  • 9. Defer ● defer is useful when one wants to ensure about function getting executed even in case of failure of the program e.g closing the files, releasing the resources etc ● defer is also helpful in debugging OR in error handling mechanism of GO func fun(a int) { fmt.Println("a =",a) } func main() { a := 5 defer fun(a) // deferred function call fmt.Println("a =", a) // Anonymous function accessing ‘a’ } // Output is // a=6 a=5 // Since the call the function fun() is deferred, it is having the //value of a as 5
  • 10. Defer ● Syntax : defer func_call( ) ● A function or method call can be prefixed with keyword defer ● Such function and argument expressions are evaluated when the statement is ● executed, but the actual call is deferred until the function containing the defer statement has finished ● No matter whether the function that contains defer statement ends normally or after panicking, the deferred function is called ● Any number of calls may be deferred; they are executed in the reverse of the order in which they were deferred func main() { a := 5 defer func(a) // deferred function call fmt.Println("a =", a) // Anonymous function accessing ‘a’ }