Pointers in GO Tutorial

Udemy Generic 728x90

Overview :  

In this article we are going to learn Pointers in GO language. As other programming languages GO have pointers. As we all know a variable contains some data or information. When a variable contains some address in it, it is considered a pointer variable.

What is Pointer :

In computer programming a pointer is a variable that stores a memory address.  Pointers are used to store the addresses of other variables or memory items.  Pointers are very useful for another type of parameter passing, usually referred to as Pass by Address.  Pointers are essential for dynamic memory allocation.

Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type.

Pointers in GO :

Like other Programming languages GO used pointer. In Go a pointer is represented using the * (asterisk) character followed by the type of the stored value. A pointer stores memory location  where a value is stored rather than the value itself. Let’s understand by the example given below.

func change(x *int) {
     *x = 5
 }
 func main() {
     x := 10
     change(&x)
     fmt.Println(x) 
 }

Output of this function will be

5

in the example above the change function is able to modify the value. This is why we used pointer. Let us take a example without pointer

func zero(x int) {
   x = 0
 }
func main() {
   x := 5
   zero(x)
   fmt.Println(x)
 }

Output of this function is:

5

In the example above zero function is not able to change the value.

The * and & operators :

In GO language a pointer is represented using the * (asterisk) character followed by the data type of the stored value. In the first example x is a pointer of data type int. *(asterisk) is also used to “dereference” pointer variables. Dereferencing a pointer gives you full access to the value where pointer is pointing. When you write *x = 0, we are saying to compiler ‘store the int 0 in the memory location x pointing to’. But if you try x = 0, this will give compiler error because you are assigning an int value to *int.

& operator is used to get a pointer of a variable. & operator is used to find address of the value stored in the variable. &x is return *int (pointer to int) because data type x is a int .

Another way to get a pointer from variable, in GO language, is to use a built-in function called new. new function takes a data type as an argument, allocates enough memory to fit a value of that data type and returns a pointer to it. Let us understand this by an example.

package main
import "fmt"

func one(xPtr *int) { 
  *xPtr = 1 
} 
func main() { 
  xPtr := new(int) 
  one(xPtr) 
  fmt.Println(*xPtr) 
}

Output of this function :

1

When we use new function it allocate values of a data type in memory. Value in this memory allocation is set to null when new function allocates it.

Examples :

Let’s understand pointer in GO with more examples.

package main
import "fmt"
type Vertex struct {
  X int
  Y int
}
func main() {
  p := Vertex{1, 2}
  q := &p
  q.X = 10
  fmt.Println(p)
}

Output of this function will be {10 2}.

Let’s take another example.

package main
import (
    "fmt"
    "unsafe"
 )
type myVariable struct {
     Value1 int
     Value2 string
 }
func main() {
     // Allocate an value of type myVariable
     myValue := &myVariable{10, "Sunil"}
 
     // Create a pointer to the memory for myValue
     //  For Display Purposes
     pointer := unsafe.Pointer(myValue)
 
     // Display the address and values
     fmt.Printf("Address: %v, Value1 : %d, Value2: %s\n",
        pointer,
        myValue.Value1,
        myValue.Value2)
 }

Output of this will be something like this.

Address: 0x10328000, Value1 : 10, Value2: Sunil

 

Source : http://www.golang-book.com/8/index.htm

http://tour.golang.org/#28

http://en.wikipedia.org/wiki/Pointer_(computer_programming)#Go

Udemy Generic 728x90

Spread the word. Share this post!