Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Implement while loop in Go (Golang)

Posted on March 8, 2023March 8, 2023 by admin

Go doesn’t have the while keyword. Instead, it has the for keyword only. However for keyword can be used to simulate the functionality the same as while.

for loop in GO basically has three parts:

for initialization_part; condition_part; increment_part {
...
}

for loop can be implemented to behave the same as while if initialization_part and increment_part can be skipped. Here is an example:

package main
import "fmt"
func main() {
i := 1
for i <= 5 {
fmt.Println(i)
i++
}
}

Output:

1
2
3
4
5
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP