C
GO

C to Go

10 lessons

Progress0%
1Variables & Types2Functions3Arrays & Slices4Structs & Methods5Pointers6Concurrency7Header Files → Packages8Error Handling9Testing10Standard Library
All Mirror Courses
C
GO
Pointers
MirrorLesson 5 of 10
Lesson 5

Pointers

Memory addresses and indirection

Introduction

In this lesson, you'll learn about pointers in Go. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
C
From C:

In C, you're familiar with memory addresses and indirection.

GO
In Go:

Go has its own approach to memory addresses and indirection, which we'll explore step by step.

The Go Way

Let's see how Go handles this concept. Here's a typical example:

GO
Go Example
package main

import "fmt"

func increment(x *int) {
    *x++
}

func main() {
    a := 5
    p := &a // p points to a
    *p = 10 // dereference and assign
    fmt.Println(a) // 10

    increment(&a)
    fmt.Println(a) // 11

    // No pointer arithmetic in Go!
    arr := []int{1, 2, 3}
    fmt.Println(arr[1]) // use index, not ptr arithmetic

    // nil pointer
    var nullP *int = nil

    // new() allocates on heap
    q := new(int)
    *q = 42
}

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
#include <stdio.h>

void increment(int *x) {
    (*x)++;
}

int main() {
    int a = 5;
    int *p = &a; /* p points to a */
    *p = 10;     /* dereference and assign */
    printf("%d\n", a); /* 10 */

    increment(&a);
    printf("%d\n", a); /* 11 */

    /* Pointer arithmetic */
    int arr[3] = {1, 2, 3};
    int *ptr = arr;
    printf("%d\n", *(ptr + 1)); /* 2 */

    /* NULL pointer */
    int *null_p = NULL;
}
Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go has & (address) and * (dereference) like C, but no pointer arithmetic

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go's nil replaces C's NULL

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go has new() for heap allocation; no malloc/free

Mirror Card
C
From C:

You may be used to different syntax or behavior.

GO
In Go:

Go auto-dereferences struct pointer fields (p.Field not p->Field)

Step-by-Step Breakdown

1. Basic Pointers

Go uses the same & and * operators as C for address-of and dereference. The syntax feels identical.

C
C
int *p = &a; *p = 10;
GO
Go
p := &a; *p = 10

2. No Pointer Arithmetic

Go deliberately omits pointer arithmetic to prevent memory corruption bugs. Use slice indices instead.

C
C
*(ptr + 2) = 99; // advance by 2 ints
GO
Go
arr[2] = 99 // use index directly
Common Pitfall
Attempting pointer arithmetic in Go is a compile error — this is intentional.

3. new() vs malloc()

Go's new(T) allocates a zero-valued T on the heap and returns *T. No need to call free — GC handles it.

C
C
int *p = malloc(sizeof(int)); *p = 42; free(p);
GO
Go
p := new(int); *p = 42 // GC frees it

Common Mistakes

When coming from C, developers often make these mistakes:

  • Go has & (address) and * (dereference) like C, but no pointer arithmetic
  • Go's nil replaces C's NULL
  • Go has new() for heap allocation; no malloc/free
Common Pitfall
Don't assume Go works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go has & and * like C — syntax is the same
  • No pointer arithmetic in Go — use slice indices
  • nil replaces NULL
  • new() replaces malloc(); GC replaces free()
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Go to practice these concepts.
PreviousNext