
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Dereferencing Pointer
A pointer is a powerful feature in the Golang programming language that allows you to work with the memory addresses.
In Golang, Dereferencing a pointer means accessing the value stored at the memory address pointer is pointing to. This concept is important in Golang as it enables you to optimize performance, share data between functions without copying, and manipulate data.
To declare a pointer, we use the * operator, and to get the memory address of a variable, we can use &(AND) operator.
Following is the way to declare a pointer and assign the memory address for Golang Deferencing Pointers
- var p *int (declares a pointer to an integer)
- p = &x (assigns the memory address of x to the pointer p)
- *p (dereferences the pointer to access the value stored at the memory address)
The Pointers themselves do not have parameters or return values, but functions that use pointers can have parameters and return values involving pointers.
Basic Dereferencing Pointers
Here, we declare an integer variable x and a pointer p that stores the memory address of x. By dereferencing the pointer p, we can access the value of x. So, we can modify the value of x by changing the value stored at the memory address pointed to by p.
Example
package main import "fmt" func main() { var x int = 64 var p *int p = &x fmt.Println("Value of x=", x) fmt.Println("Address of x=", &x) fmt.Println("Value of p=", p) fmt.Println("Dereferenced value of p=", *p) *p = 21 fmt.Println("New value of x=", x) }
Output
Following is the output of the above program
Value of x= 64 Address of x= 0xc000124010 Value of p= 0xc000124010 Dereferenced value of p= 64 New value of x= 21
Dereferencing Struct Pointers
When you have a pointer to a struct, you can use the * operator or the shorthand -> syntax to access the struct's fields.
Example
package main import "fmt" type Student struct { name string age int } func main() { p := &Student{"Indu", 18} fmt.Println("Name=", p.name) fmt.Println("Age=", (*p).age) }
Output
Following is the output of the above program
Name= Indu Age= 18
Passing and Dereferencing Pointers in Functions
Functions can take pointers as parameters and dereference them to modify the original values.
Example
package main import "fmt" func updateValue(val *int) { *val = 100 } func main() { num := 25 updateValue(&num) fmt.Println("Updated Value=", num) }
Output
Following is the output of the above program
Updated Value= 100