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