Skip to content

Latest commit

 

History

History

pointers

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Go Pointers Icon

📌 Go Pointers: An Introduction

Welcome to the world of Go (Golang) pointers! This README provides a beginner-friendly introduction to pointers in Go, explaining their purpose, usage, and providing examples to illustrate their behavior.

❓ What are Pointers?

In Go, pointers are your trusty guides to memory locations. They allow you to indirectly access and modify the value of a variable by storing the memory address where the value is stored. Pointers are particularly useful when dealing with large data or when you need to update a variable from different parts of your program.

📍 Pointer Declaration and Initialization

To declare a pointer in Go, simply add a * symbol before the type name. Here's an example:

var ptr *int

In this example, ptr is a pointer that can hold the memory address of an int value. However, it is currently uninitialized and doesn't point to any specific memory location.

To initialize a pointer with the memory address of a variable, you use the address-of operator &. See this example:

⚙️ Dereferencing Pointers

To access the value pointed to by a pointer, you use the dereference operator *. It allows you to retrieve the value stored at the memory location pointed to by the pointer. Here's an example:

var num = 42
var ptr = &num
fmt.Println(*ptr) // Output: 42

In this example, by using *ptr, we dereference the ptr pointer to access the value stored at the memory location it points to, resulting in the output 42.

🔄 Modifying Values through Pointers

One significant advantage of pointers is the ability to modify the value of a variable indirectly. By dereferencing the pointer, we can access and modify the original value. Let's see an example:

var num = 42
var ptr = &num

*ptr = 100

fmt.Println(num) // Output: 100

In this example, we modify the value of num indirectly by assigning a new value (100) to the dereferenced pointer *ptr. As a result, the value of num changes to 100.

🚀 Want Start

To get started with the code examples and projects in this repository, follow these steps:

  1. Clone the repository to your local machine.
    git clone https://github.com/anshitmishra/golang-tutorial.git
    
    

🤝 Contributing

We welcome contributions to this repository! If you have ideas for new projects, improvements to existing projects, or additional code examples, feel free to submit a pull request. Let's collaborate and make this repository even better!

Please ensure that your contributions align with the purpose and standards of this repository. For more details, refer to the CONTRIBUTING file.

📄 License

This repository is licensed under the MIT License. You are free to use, modify, and distribute the code in this repository for personal or commercial purposes. However, we assume no liability for any misuse or damages caused by the use of this code. Please review the license file for more details.