DEV Community

Cover image for Understanding Reference Types & Pointers in Go
Ganesh Kumar
Ganesh Kumar

Posted on

Understanding Reference Types & Pointers in Go

#go

Hello, I'm Ganesh Kumar. I'm working on git-lrc: a Git hook for Checking AI generated code.
AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

In my previous post, we learned about defer in Go—where and how it is used. Now, let's dive into reference types.

Reference Types

There are five main reference types in Go:

  1. Pointers
  2. Slices
  3. Maps
  4. Channels
  5. Functions

The basic difference between value types and reference types is that value types are copied when assigned to a variable or passed to a function, while reference types are not.

Now, let's learn about Pointers.

Pointers

Go includes a concept called pointers. Using pointers, we can directly access and work with the memory address of a variable.

package main

import "fmt"

func main() {
    i, j := 42, 2701

    p := &i         // p points to i
    fmt.Println(*p) // Read i through the pointer (dereferencing)

    *p = 21         // Set i through the pointer
    fmt.Println(i)  // See the new value of i

    p = &j          // p now points to j
    *p = 13         // Set j through the pointer
    fmt.Println(j)  // See the new value of j
    fmt.Println(p)  // Print the memory address stored in p
}

Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go 
42
21
13
0xc000012130

Enter fullscreen mode Exit fullscreen mode

In Go, & is the address-of operator, and * is the dereference operator.

Therefore, p := &i means that p is a pointer to the variable i.

Conclusion

In this we learned about pointers and how it is used.

Now let's learn about struct data type.

git-lrc

👉 Check out: git-lrc
Any feedback or contributors are welcome! It’s online, open-source, and ready for anyone to use.
⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit




AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • 🔁 Build a

Top comments (0)