WEB DEVELOPER SITE
htmLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Pointers


Points in the Go language are easy to learn, and using pointers in the Go language makes it easier to perform tasks.

Next let's take a step by step to learn the Go language pointer.

We all know that variables are a convenient placeholder for referencing computer memory addresses.

The address of the Go language is &,Use before returning to a variable will return the memory address of the corresponding variable.

The following example demonstrates the variable's address in memory:

example

package main

import "fmt"

func main() {
   var a int = 10  

   fmt.Printf("Variable address: %x\n", &a  )
}

Execute the above code output as:

Address of the variable: 20818a220 

Now we have seen what a memory address is and how to access it. Next we will introduce the pointer specifically.


What is pointer

A pointer variable points to the memory address of a value.

Similar to variables and constants, you need to declare a pointer before using the pointer. The pointer declaration format is as follows:

var var_name *var-type

var-type is the pointer type, var_name is the pointer variable name, and * is used to specify the variable as a pointer. The following is a valid pointer declaration:

var ip *int /* Point to integer*/
var fp *float32 /* Point to floating point */

In this case, this is a pointer to int and float32.


How to use pointers

Pointer usage process:

  • Define a pointer variable.
  • Assign a pointer variable.
  • Access the value of the pointer variable to the address.

Insert the pointer type with an * (prefix) to get the pointer to the content.

example

package main

import "fmt"

func main() {
   var a int= 20   /* Declare actual variables*/
   var ip *int        /* Declare pointer variable */

   ip = &a  /* Pointer variable storage address */

   fmt.Printf("a The address of the variable is: %x\n", &a  )

   /* Pointer variable storage address */
   fmt.Printf("ip Pointer address for variable storage: %x\n", ip )

   /* Use pointers to access values */
   fmt.Printf("*ip Value of the variable: %d\n", *ip )
}

The above example execution output is:

a The address of the variable is: 20818a220
ip Pointer address for variable storage: 20818a220
*ip Value of the variable: 20

Go null pointer

When a pointer is defined without any variables assigned to it, it is Value nil.

nil pointers are also known as null pointers.

nil is conceptually the same as null, none, nil, or NULL in other languages, and refers to zero or null values.

A pointer variable is usually abbreviated as ptr.

View the following example:

example

package main

import "fmt"

func main() {
   var  ptr *int

   fmt.Printf("ptr Value : %x\n", ptr  )
}

The output of the above example is:

ptr Value : 0

The null pointer judges:

if(ptr != nil)     /* ptr Not a null pointer */
if(ptr == nil)    /* ptr Is a null pointer */

Go Pointers in Detail

There are basic thoughts related with pointer which should be perfect to a Go software engineer −

Sr.No Concept & Description
1 Go - Array of pointers

You can define an array of pointers to store the address.

2 Go - Pointer to pointer

Support for pointers to pointers .

3 Passing proposals to highlights in Go

Passing a reference or an address, you can change its value when the function is called.