SlideShare uma empresa Scribd logo
1 de 11
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
 
JavaScript: Patterns, Part 3
JavaScript: Patterns, Part  3JavaScript: Patterns, Part  3
JavaScript: Patterns, Part 3Chris Farrell
 
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
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
storage class
storage classstorage class
storage classstudent
 
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 DevelopersJayaram Sankaranarayanan
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
 
Programming Global variable
Programming Global variableProgramming Global variable
Programming Global variableimtiazalijoono
 

Mais procurados (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
 

Semelhante a Pointers & functions

Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdfNehaSpillai1
 
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.pptxChetanChauhan203001
 
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).pptxPrabha Karan
 
what-is-python-presentation.pptx
what-is-python-presentation.pptxwhat-is-python-presentation.pptx
what-is-python-presentation.pptxVijay Krishna
 
What is Paython.pptx
What is Paython.pptxWhat is Paython.pptx
What is Paython.pptxParag Soni
 
python-presentation.pptx
python-presentation.pptxpython-presentation.pptx
python-presentation.pptxVijay Krishna
 
Python functions and loops
Python functions and loopsPython functions and loops
Python functions and loopsthirumurugan133
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfRITHIKA 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 pythonGandaraEyao
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptxSulekhJangra
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewMarkus Schneider
 

Semelhante a 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
 
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-python-presentation.pptx
what-is-python-presentation.pptxwhat-is-python-presentation.pptx
what-is-python-presentation.pptx
 
What is Paython.pptx
What is Paython.pptxWhat is Paython.pptx
What is Paython.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
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 

Mais de Manjitsing Valvi

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 worldManjitsing Valvi
 
Digital marketing channels
Digital marketing channelsDigital marketing channels
Digital marketing channelsManjitsing Valvi
 
Digital marketing techniques
Digital marketing techniquesDigital marketing techniques
Digital marketing techniquesManjitsing Valvi
 
Social media marketing & managing cybersocial campaign
Social media marketing & managing cybersocial campaignSocial media marketing & managing cybersocial campaign
Social media marketing & managing cybersocial campaignManjitsing Valvi
 
Creating marketing effective online store
Creating marketing effective online storeCreating marketing effective online store
Creating marketing effective online storeManjitsing 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 enginesManjitsing Valvi
 
Digital marketing managing cybersocial campaign
Digital marketing managing cybersocial campaignDigital marketing managing cybersocial campaign
Digital marketing managing cybersocial campaignManjitsing Valvi
 

Mais de 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
 

Último

(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 

Último (20)

(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 

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’ }