Posts

Showing posts with the label loops in go

Loops in GoLang

Image
Loops Loops are one of the iterative constructs used in all programming languages to perform some task iteratively or to recurse over a data structure like a map or an array. Many languages provide a set of FOR, WHILE and DO WHILE   loop but GoLang provides a different approach towards these three loops. The only looping constructs available in Go is FOR.   Let's see how we can achieve different looping scenarios using GO. Classic For loop Following is an example of how we can write the classic for loop with initialization, condition and increment step. package main import "fmt" func main() { var i int //for initialization;condition; increment/decrement for i = 0; i < 10; i++ { fmt.Println("Inside the loop") } } For with just condition (While loop) A while loop kind construct can be achieved using the for loop with just a condition and performing the initialization step before the loop and increment/decrement step insi