DEV Community

Cover image for Go Programming Language: Everything You Need To Know About It (Part 7)
Ganesh Kumar
Ganesh Kumar

Posted on • Edited on

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

#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 data types in Go.

Now, we will learn about loops and control flow in Go.

Loops

In Go, loops are used to execute a block of code repeatedly until a specific condition is met.

Unlike other languages that have while or do-while loops, Go keeps it simple: for is the only looping construct available.

The Three-Component Loop

The most common form of a loop includes an init statement, a condition expression, and a post statement.

package main
import "fmt"

func main() {
    sum := 0
    // i := 0 (initialization); i < 10 (condition); i++ (post statement)
    for i := 0; i < 10; i++ {
         sum += i
    }
    fmt.Println("The sum is:", sum)
}

Enter fullscreen mode Exit fullscreen mode

Key Fixes:

  • Variable Declaration: In your snippet, i was not declared. In Go, you must use i := 0 or declare it beforehand.
  • Typo: You had sum += l (lowercase L) instead of sum += i.
  • Formatting: fmt.Printf requires a format string; for simple values, fmt.Println is usually easier.

Conditional Control Flow

Conditional statements allow your program to make decisions based on specific criteria. Go supports the standard if, else if, and else blocks.

The if Statement

Go’s if statements are straightforward. One unique feature is that you don't need parentheses () around the condition, but braces {} are mandatory.

package main
import "fmt"

func main() {
    var temp = 20

    if temp > 30 {
        fmt.Println("Hot")
    } else if temp > 20 {
        fmt.Println("Warm")
    } else {
        // Since temp is 20, it doesn't meet the >20 condition
        fmt.Println("Cold")
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go 
Cold

Enter fullscreen mode Exit fullscreen mode

Conclusion

Control flow is the backbone of logic in Go.

By mastering the for loop and conditional if statements, you can handle almost any iterative or decision-making task in your applications.

Go's design encourages clean, readable code by limiting the number of ways you can perform these operations.

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)