DEV Community

Cover image for How Slices Work Internally in Go
Ganesh Kumar
Ganesh Kumar

Posted on • Edited on

How Slices Work Internally 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.

Here is the corrected version of your blog post with improved grammar and flow, while maintaining your personal style:

In my previous post, I explained how to work with arrays and use pointers with arrays.
Now let's learn about slices in go.

What is Slices

Slices are dynamic arrays.

Slices use pointers to refer to the underlying array.

Here is how you can create slices.

How to create slices

Initialize array and create slice from it.

For creating slice from array, we use [start:end] notation.

start is the starting index of the slice.

end is the ending index of the slice.

import "fmt"

func main() {
    s := [5]int{10, 20, 30, 40,50}
    fmt.Println(s)
    var s1 []int = s[1:3]
    fmt.Println(s1)
}
Enter fullscreen mode Exit fullscreen mode

Output:

[10 20 30 40 50]
[20 30]
Enter fullscreen mode Exit fullscreen mode

How Slices Work Internally

Slices will be having 3 components.

  1. Pointer to the underlying array.
  2. Length of the slice.
  3. Capacity of the slice.

It is little bit confusing, let's understand with above example.

[10, 20, 30, 40, 50]
Enter fullscreen mode Exit fullscreen mode

Now by above image we can understand how it works internally.

Conclusion

In this we learned about slice data type and how it works internally.

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 (2)

Collapse
 
clarabennettdev profile image
Clara Bennett

Nice overview of slice internals! One thing worth mentioning is the gotcha with append β€” when a slice exceeds its capacity, Go allocates a new underlying array. This means two slices that originally shared the same backing array can diverge silently after an append. It's one of those things that trips people up in production when they're mutating slices passed between functions. The diagram really helps make the pointer/length/capacity relationship click.

Collapse
 
ganesh-kumar profile image
Ganesh Kumar

Thanks for letting me know.
I have started learning in depth on these concepts.

Will surely mention these gotcha in upcoming articles.