DEV Community

Ganesh Kumar
Ganesh Kumar

Posted on

Go Programming Language: Everything You Need To Know About It (Part 8)

#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, 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)
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  1. for {} starts an infinite loop.
  2. if sum > 10 checks the condition.
  3. break exits the loop immediately when the condition is true.
  4. sum++ increments the counter in each iteration.

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
The sum is: 11
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

Key points:

  • The sum function returns two values: int and error.
  • errors.New creates a basic error object.
  • In main, we capture both result and err.
  • if err != nil is the standard way to check for errors. If err is not nil, something went wrong.

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
21
Enter fullscreen mode Exit fullscreen mode

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")
    }
}
Enter fullscreen mode Exit fullscreen mode

Key points:

  • Go's switch is like if-else-if-else but cleaner.
  • Unlike C/C++, you don't need break at the end of each case. Go breaks automatically.
  • default runs if no other case matches.

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
2
Choice 2
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this part, we learned about:

  • Using break condition inside loops
  • Standard error handling pattern with multiple return values
  • Usage of switch statements for cleaner conditional logic

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)