Programmer’s Career

Programming Career of a software development engineer

Follow publication

Member-only story

Pointers You Should Know in Golang

Exploring Pointers in Go: A Comprehensive Guide

from github.com/MariaLetta/free-gophers-pack

Hello, here is Wesley, Today’s article is about Pointers in Go. Without further ado, let’s get started.💪

1.1 Pointer Basics

Introduce what a pointer is, how it works in memory, and why we need pointers.

In the Go language, if we want to store an integer, we use the int type, and if we want to store a string, we use the string type. But how do we store a memory address? What data type should we use?

The answer is a pointer.

When we declare a variable, the computer allocates a block of memory for it and stores the data in that block. A pointer is a variable that stores the memory address of another variable. Through a pointer, we can indirectly access or modify the value stored at that memory address.

For example:

var a int = 42
var p *int = &a // p is a pointer that points to variable a's memory address

In this code, a is an integer variable, and p is a pointer that points to a. The &a operator represents the memory address of a, and p stores that address.

Responses (1)

Write a response