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, we have learned about looping and conditions.
Now we will learn about Conditional Control Flow.
Conditional Control Flow in Loops
Here is an example of an infinite loop with a break statement based on a condition.
This pattern is commonly used when you want to terminate the loop based on a check inside the loop body, rather than a pre-condition.
package main
import "fmt"
func main() {
sum := 0
// infinite loop
for {
if sum > 10 {
break
}
sum++
}
fmt.Println("The sum is:", sum)
}
In this example:
-
for {}starts an infinite loop. -
if sum > 10checks the condition. -
breakexits the loop immediately when the condition is true. -
sum++increments the counter in each iteration.
Output:
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
The sum is: 11
Error Handling Patterns
Go handles errors explicitly using return values.
package main
import (
"errors"
"fmt"
)
// sum returns the sum and an error if x is too small
func sum(x int, y int) (int, error) {
if x > 10 {
return x + y, nil // nil means no error
}
// Return the value and a new error
return x + y, errors.New("x is less than 10")
}
func main() {
result, err := sum(11, 10)
// Standard error check
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(result)
}
Key points:
- The
sumfunction returns two values:intanderror. -
errors.Newcreates a basic error object. - In
main, we capture bothresultanderr. -
if err != nilis the standard way to check for errors. Iferris not nil, something went wrong.
Output:
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
21
Switching Case
Switch statements express conditionals across many branches.
package main
import (
"fmt"
)
func main() {
var num int
fmt.Print("Enter number: ")
fmt.Scan(&num)
switch num {
case 1:
fmt.Println("Choice 1")
case 2:
fmt.Println("Choice 2")
default:
fmt.Println("default")
}
}
Key points:
- Go's switch is like
if-else-if-elsebut cleaner. - Unlike C/C++, you don't need
breakat the end of each case. Go breaks automatically. -
defaultruns if no other case matches.
Output:
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
2
Choice 2
Conclusion
In this part, we learned about:
- Using
breakcondition inside loops - Standard error handling pattern with multiple return values
- Usage of
switchstatements for cleaner conditional logic
π 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:
HexmosTech
/
git-lrc
Free, Unlimited AI Code Reviews That Run on Commit
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)