SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Plus 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
 

Plus 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
 

Dernier

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
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
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
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
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 

Dernier (20)

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
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
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
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...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
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
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
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
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 

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