DEV Community

Cover image for Go Slices: Loop over append function
Ganesh Kumar
Ganesh Kumar

Posted on

Go Slices: Loop over append function

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 append function algorithm

Now let's see how loop works with append function

Loop over append function

Let's assign the power of 2 to a slice and print it.

With normal loop we can print the slice.

We basicaly loop over the length of the slice using the index.

package main

import "fmt"



var power = []int{1,2,4,8,16,32}

func main() {
    for i:=0; i<len(power); i++ {
        fmt.Printf("2^%d = %d\n", i, power[i])
    }
}
Enter fullscreen mode Exit fullscreen mode

This will output:

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
Enter fullscreen mode Exit fullscreen mode

Go inbuilt range function

Go provides a very simple way to loop over a slice.

package main

import "fmt"



var power = []int{1,2,4,8,16,32}

func main() {
    for i,v := range power {
        fmt.Printf("2^%d = %d\n", i, v)
    }
}
Enter fullscreen mode Exit fullscreen mode

Where i is the index and v is the value of the element.

Output:

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
Enter fullscreen mode Exit fullscreen mode

we can also define the index as _ if we don't want to use it.

package main

import "fmt"



var power = []int{1,2,4,8,16,32}

func main() {
    for _,v := range power {
        fmt.Printf("%d\n", v)
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
4
8
16
32
Enter fullscreen mode Exit fullscreen mode

Conclusion

We understood how to loop over a slice using normal index based loop and range 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…




https://github.com/golang/go/blob/master/src/runtime/slice.go

Top comments (0)