Thursday 9 May 2019

Go language: Pointers


A pointer is used to hold the address of a variable.

Syntax
var pointerName *dataType

Example
var intPtr *int
var a int = 10
intPtr = &a

'intPtr' can able to hold the address of an integer variable. The & operator gives the address of a variable.

App.go
package main

import "fmt"

func main() {
 var intPtr *int

 var a int = 10

 intPtr = &a

 fmt.Println("Value of a is : ", a)

 fmt.Println("\na is stored at address : ", intPtr)
 fmt.Println("Value of a is : ", *intPtr)
}

Output
Value of a is :  10

a is stored at address :  0xc000014058
Value of a is :  10


Previous                                                 Next                                                 Home

No comments:

Post a Comment