DEV Community

Cover image for Go Slices: Empty Slices, Make, and Variable Declaration
Ganesh Kumar
Ganesh Kumar

Posted on

Go Slices: Empty Slices, Make, and Variable Declaration

#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 empty data is represented in slices.

How Empty Slice is Represented

package main

import "fmt"

func main() {
    var s []int
    fmt.Println(s, len(s), cap(s))
    if s == nil {
        fmt.Println("s is nil")
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

[] 0 0
s is nil
Enter fullscreen mode Exit fullscreen mode

len and cap are 0 because the slice is not initialized.

When we initialize a slice, it will have a defined length and capacity.

The underlying pointer will point to the first element of the backing array, and both the length and capacity will be set to the number of elements in that array.

Initializing a Slice with Make

make is a predefined function in Go used to create and initialize slices, maps, and channels.

Reference variables like slices are typically initialized using the make command from Go's built-in functions.

package main

import "fmt"

func main() {
    a := make([]int, 5)
    fmt.Println(a, len(a), cap(a))

    b := make([]int, 5, 10)
    fmt.Println(b, len(b), cap(b))
}
Enter fullscreen mode Exit fullscreen mode

Output:

[0 0 0 0 0] 5 5
[0 0 0 0 0] 5 10
Enter fullscreen mode Exit fullscreen mode

This allows you to create a slice with a specific length and capacity.

make([]T, len, cap)

  • For a := make([]int, 5), it creates a slice of length 5 and capacity 5.
  • For b := make([]int, 5, 10), it creates a slice of length 5 and capacity 10.

Variable Declaration

You can also initialize a slice directly using a slice literal:

package main

import "fmt"

func main() {
    a := []int{1, 2, 3, 4, 5}
    fmt.Println(a, len(a), cap(a))
}
Enter fullscreen mode Exit fullscreen mode

Output:

[1 2 3 4 5] 5 5
Enter fullscreen mode Exit fullscreen mode

For this slice, the pointer points to the first element of the underlying array, and the length and capacity are set to the number of elements provided.

Conclusion

We learned about empty slices, initializing slices with make, and declaring slices using literals.

we explored how data is stored in a slice and examined slice modifications to understand how they work under the hood.

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)