Go Programming Language Pointers

Go Programming Language Pointers

In Go programming, pointers feature is available and explains here how to use pointers.

A pointer is a programming language object whose value refers to another value stored else-where in the memory using it’s address.

Address of memory for any value can be accessed using & symbol.

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	var a int                                                              	 
	a = 20                                                                 	 
                                                                           	 
	// prints value of a                                                   	 
	fmt.Printf("Value of a: %d\n", a)                                      	 
                                                                           	 
	// prints address of x                                                 	 
	fmt.Println("Address of a: ",&a)                                       	 
}	
Output:
$ go build pointers.go
$ ./pointers
Value of a: 20
Address of a:  0xc42000a310

How to define pointer variable in Go ?

Below code is used to define integer pointer variable:

var a *int

Setting value to the pointer variable

*a = 10

Setting memory address to the pointer variable

a = memory_address

Go Pointers Example

package main                                                               	 
                                                                           	 
import "fmt"                                                               	 
                                                                           	 
func main() {                                                              	 
                                                                           	 
	var a int                                                              	 
	a = 20           
//define pointer variables ptr and val                                                      	 
	var ptr *int                                                           	 
	val :=  new(int)                                                       	 
	ptr = new(int)                                                         	 
                                                                           	 
	// setting value to the pointer variable                               	 
	*ptr = a                                                               	 
	fmt.Printf("Address of pointer variable 'ptr': %d\n", ptr)             	 
	fmt.Printf("Value of pointer variable 'ptr': %d\n", *ptr)              	 
                                                                           	 
	//seting address                                                       	 
	val = &a                                                               	 
                                                                           	 
	// setting address of val variable to pointer variable                 	 
	fmt.Printf("Address of pointer variable 'val': %d\n", val)             	 
	fmt.Printf("Value of pointer variable 'val': %d\n", *val)              	 	 
}
Output:
$ go build pointers.go
$ ./pointers
Address of pointer variable 'ptr': 842350961080
Value of pointer variable 'ptr': 20
Address of pointer variable 'val': 842350961032
Value of pointer variable 'val': 20