GO Tutorial



POINTERS IN GO


Pointers in Go

Pointers store the memory address of a variable instead of its value. They allow you to directly manipulate the data stored in memory — a powerful feature in Go for performance and flexibility.

Basic Pointer Syntax

var x int = 42
var p *int = &x  // p stores address of x

fmt.Println(x)   // 42
fmt.Println(p)   // memory address of x
fmt.Println(*p)  // 42 (value at address p)
    

Explanation: By passing a pointer to x, the increment function directly modifies x’s value in memory. This is efficient and useful for changing variables outside the function scope.

Why Use Pointers?

  • Modify variables directly in functions without returning them.
  • Improve performance by avoiding copying large data structures.
  • Work with dynamic data structures like linked lists, trees, etc.

Quick Recap

Symbol Meaning Example
& Address of operator (gets memory address) p = &x
* Dereference operator (gets value at memory address) val = *p

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review