Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

Implement while loop in Go (Golang)

Posted on March 8, 2020March 8, 2020 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
  • Follow @golangbyexample

    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

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme