Go (Golang) / Go Data Structures

Understanding Pointers in Go

In this tutorial, you will get to grips with pointers in Go. Pointers can be a difficult concept to grasp, but they are a powerful tool in Go's arsenal.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers essential data structures in Go, including arrays, slices, and maps.

Understanding Pointers in Go

1. Introduction

  • This tutorial aims to provide an easy-to-understand guide on how to use pointers in Go programming language. Pointers can be intimidating for new programmers, but with a clear understanding of their concept, they can become a powerful tool in your coding arsenal.
  • By the end of this tutorial, you will understand what pointers are, how to declare and use them, and why they are useful in Go.
  • Prerequisites: Basic knowledge of Go programming is required.

2. Step-by-Step Guide

  • A pointer in Go is a variable that stores the memory address of another variable. The variable that stores the address is the pointer, and the variable whose address is being stored is the pointee.
  • Pointers are declared using the * operator before the type of the stored value. The & operator is used to get the address of a variable.
  • Here is how you can declare a pointer: var p *int, where p is a pointer to an int.
  • Here is how you can get the address of a variable: p = &i, where i is an int variable and p is the address of i.

3. Code Examples

  • Let's see an example:
package main

import "fmt"

func main() {
   var i int = 10   // declare int variable
   fmt.Printf("Value of i: %d\n", i)

   var p *int       // declare pointer to int
   p = &i           // assign address of i to p
   fmt.Printf("Address of i: %d\n", p)
}
  • In the above code:
  • We first declare an int variable i and assign a value of 10 to it.
  • Then we declare a pointer p to an int.
  • We assign the address of i to p using the & operator.
  • The output will be the value of i and the memory address of i.

4. Summary

  • Pointers in Go are variables that store the address of another variable.
  • They are declared using the * operator, and the & operator is used to store the address of another variable in a pointer.
  • Pointers are useful for sharing large data structures without copying the data, and for changing multiple variables in a function.

5. Practice Exercises

  1. Declare two int variables, assign values to them, declare a pointer for each, and print their addresses.
  2. Write a function that takes a pointer to an int as a parameter, changes the value of this int, and prints the new value in the main function.
  3. Write a function that takes a pointer to an array as a parameter, changes an element in this array, and prints the new array in the main function.

Solutions:

package main

import "fmt"

func main() {
   var i1, i2 int = 5, 10
   var p1, p2 *int

   p1 = &i1
   p2 = &i2

   fmt.Printf("Address of i1: %d, Address of i2: %d\n", p1, p2)
}
package main

import "fmt"

func changeValue(p *int) {
   *p = 20
}

func main() {
   var i int = 10
   fmt.Printf("Before: %d\n", i)
   changeValue(&i)
   fmt.Printf("After: %d\n", i)
}
package main

import "fmt"

func changeArray(p *[3]int) {
   (*p)[1] = 20
}

func main() {
   arr := [3]int{1, 2, 3}
   fmt.Printf("Before: %v\n", arr)
   changeArray(&arr)
   fmt.Printf("After: %v\n", arr)
}

Keep practicing and experimenting with pointers in different scenarios to get a solid understanding of them.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help