DEV Community

Cover image for Go Slices: 2D Slices and Starting with the Append Function
Ganesh Kumar
Ganesh Kumar

Posted on

Go Slices: 2D Slices and Starting with the Append Function

#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 about empty slices, initializing slices with make, and declaring slices using literals.

Now we will see how to initialize 2D slices and how to append to a slice.

Initializing 2D Slices

package main

import (
    "fmt"
    "strings"
)

func main() {

    ticTacToeBoard := [][]string{
        {"_", "_", "_"},
        {"_", "_", "_"},
        {"_", "_", "_"},
    }

    ticTacToeBoard[0][0] = "X"
    ticTacToeBoard[2][2] = "O"
    fmt.Println(ticTacToeBoard)
    for i := 0; i < len(ticTacToeBoard); i++ {
        fmt.Println(strings.Join(ticTacToeBoard[i], " "))
    }
}
Enter fullscreen mode Exit fullscreen mode

We are initializing a 2D slice of strings with 3 rows and 3 columns.

We can modify the values of the slice using the index variable[x][y].

Finally, we are printing the slice using a loop.

Each loop iteration prints a row of the slice.

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go 
[[X _ _] [_ _ _] [_ _ O]]
X _ _
_ _ _
_ _ O
Enter fullscreen mode Exit fullscreen mode

Appending to a Slice

Slices is dynamic arrays. We can append to a slice using the append function.

We can append any data type to a slice.

Simple example:

package main

import "fmt"

func main() {
    a := []int{1, 2, 3, 4, 5}
    fmt.Println(a)

    a = append(a, 6)
    fmt.Println(a)
}
Enter fullscreen mode Exit fullscreen mode

Output:

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

Before we start going further, let's understand how the append function works.

As we know, slices are dynamic arrays.

When we append to a slice, the slice is resized.

Let's check how appending will resize the slice.

package main

import "fmt"

func main() {
    a := []int{1, 2, 3, 4, 5}
    printSlice(a)

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

If you have seen previous article here is the link.

There i have explained how slices work internally.

So, With that context, there result should be len=6 cap=6 [1 2 3 4 5 6] after appending 6 to the slice.

Actual Output:

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

Why cap is 10?

To understand this, let's see how the append function works.

In the next article, let's explore why this happens.

Conclusion

Understanding how slices work is important for writing efficient Go code.

We also found the capacity of the slice is not always equal to the length of the slice after appending. Will explore why it happens by understanding the core algorithm of append function.

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)