golang tutorial - Pointers In Golang | Golang Pointers - golang - go programming language - google go - go language - go program - google language



Pointers In Golang

  • Pointers in Go are easy and fun to learn. Some Go programming tasks are performed more easily with pointers, and other tasks, such as call by reference, cannot be performed without using pointers.
  • Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory.
 if statement
package main

import "fmt"

func main() {
   var sampleVariable int = 100   

   fmt.Printf("Address of sampleVariable value: %x\n", &a  )
}
click below button to copy the code. By - golang tutorial - team

Output for the above code

Address of sampleVariable value: 105480
click below button to copy the code. By - golang tutorial - team

What Are Pointers?

  • A pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address.
  • A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
  • Typical real-time example, a page number in a book's index could be considered a pointer to the corresponding page;
  • Dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on the indexed page.

pointer variable declaration syntax:

var var_name *var-type
click below button to copy the code. By - golang tutorial - team
  • type is the pointer's base type; it must be a valid C data type
  • var-name is the name of the pointer variable.
  • The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication.
var sampleVariable *int        /* pointer to an integer */
var floatVariable *float32    /* pointer to a float */
click below button to copy the code. By - golang tutorial - team
 if statement

A sample example program in "C" language for pointers

How to use Pointers?

  • Define a pointer variable 
  •  assign the address of a variable to a pointer and 
  • finally access the value at the address available in the pointer variable.
  • This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
package main

import "fmt"

func main() {
   var sampleVariable int= 140   /* actual variable declaration */
   var Pointr *int        /* pointer variable declaration */

   Pointr = & sampleVariable  /* store address of a in pointer variable*/

   fmt.Printf("Address of sampleVariable value: %x\n", & sampleVariable  )

   /* address stored in pointer variable */
   fmt.Printf("Address stored in Pointr variable: %x\n", Pointr )

   /* access the value using the pointer */
   fmt.Printf("Value of *Pointr variable: %d\n", *Pointr )
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Output of the above golang program

Address of sampleVariable value: 50228001
Address stored in Pointr variable: 50228001
Value of *Pointr variable: 140

nil Pointers in Go

  • Go compiler assign a Nil value to a pointer variable in case you do not have exact address to be assigned.
  • This is done at the time of variable declaration. A pointer that is assigned nil is called a nil pointer.
  • The nil pointer is a constant with a value of zero defined in several standard libraries.
package main

import "fmt"

func main() {
   var  Pointr *int

   fmt.Printf("The value of Pointr is : %x\n", Pointr  )
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

output for the above golang program

The value of Pointr is 0
  • None of the programs were not permitted to access memory at address 0 because that memory is reserved by the operating system.
  • The memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location.
  • if a pointer contains the nil (zero) value, it is assumed to point to nothing.

nil pointer checks

if(Pointr != nil)     /* succeeds if Pointr is not nil */
if(Pointr == nil)    /* succeeds if Pointr is null */
click below button to copy the code. By - golang tutorial - team

Related Searches to Go Pointers In Golang