DEV Community

Cover image for How Re-slicing Affects Slice Pointers in Go
Ganesh Kumar
Ganesh Kumar

Posted on • Edited on

How Re-slicing Affects Slice Pointers in 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, I explained how to create slices and how it works internally.

Using Structs with Slices

We can store any data type in a slice.

For example, we can store a struct in a slice.

package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    people := []Person{
        {Name: "John", Age: 30},
        {Name: "Jane", Age: 25},
    }
    fmt.Println(people)
}
Enter fullscreen mode Exit fullscreen mode

The output of the code will be having data with {} output inside square brackets[].

Output:

[{John 30} {Jane 25}]
Enter fullscreen mode Exit fullscreen mode

Re-Slices The Existing Slice

When we do slicing from new slice with different start point then it will create new slice with different pointer.

if we change the pointer then data the previous pointer will be losted.

To identify how it works let's see the below example.

package main

import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    printSlice(s)

    s = s[1:5]
    printSlice(s)

    s = s[2:]
    printSlice(s)

    s = s[:1]
    printSlice(s)
}
func printSlice(s []int) {
    fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
Enter fullscreen mode Exit fullscreen mode

Here is the visual representation.

Output:
This is the actual output of the code.

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
len=10 cap=10 [1 2 3 4 5 6 7 8 9 10]
len=4 cap=9 [2 3 4 5]
len=2 cap=7 [4 5]
len=1 cap=7 [4]
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this we learned about storing data in slices and did some slice modifications to understand how it works.

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

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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 habit, ship better code. Regular review β†’ fewer bugs β†’ more robust code β†’ better results in your team.
  • πŸ”— Why git? Git is universal. Every editor, every IDE, every AI…




Top comments (0)