
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 ]) } } This will output: 2^0 = 1 2^1 = 2 2^2 = 4 2^3 = 8 2^4 = 16 2^5 = 32 Go inbuilt range function Go provides a very simple way to loop over a slice. package mai
Continue reading on Dev.to Tutorial
Opens in a new tab



