Tuesday, September 8, 2020

Golang: Loops

 

package main
import (
    "fmt"
    "time"
)
func main(){
    for i:=0;i<10;i++{
        if i==0{
            continue
        }
        fmt.Println("Inside classic for loop, value is:",i)
    }
    fmt.Println("\n\n")

    j:= -20
    for j != 0{
        fmt.Println("Inside single condition loop,value of j is:",j)
        j++
    }
    fmt.Println("\n\n")

    loopTimer:= time.NewTimer(time.Second * 9)
    //time out for loop.
    for{
        fmt.Println("Inside the infinite loop!")
        <-loopTimer.C 
        break
    }
}
//last loop uses looptime.C which is a channel listening to timer.

last loop uses looptime.C which is a channel listening to timer.

No comments:

Post a Comment