Skip to main content

Controlling Program Flow of golang

 


Overview

In this blog I am going to talk about the topic below.

Loop

You can use loop as below. here i variable is declared inside the loop so the value will be valid in side the loop.

 Example 

      
    package main

    import (
        "fmt"
    )

    func main() {
        for i:=0;i<5;i++{
            fmt.Println(i)
        }

    }
    
  

output :

    0
    1
    2
    3
    4
    5
  

Break in Loop

what Break statement dose is it will break out from the loop. 
      package main

      import (
          "fmt"
      )

      func main() {
          for i:=0;i<5;i++{
              fmt.Println(i)
              if i==2 {
                  break
              }
          }

      }
  

output :

    0
    1
    2
in the example you can see when loop comes to the point when i == 2 then it break out from the loop and end the program. this why it out put the value of i until it is 2.

Continue in Loop

what Continue statement dose is it will break out from the loop but will continue with the next iteration that loop. but it will ignore every code after that statement. 
  package main
  import ("fmt")

  func main() {
      for i:=0;i<5;i++{
          fmt.Println(i)
          if i==2 {
              continue	
          }
          fmt.Println("hello")
      }
  }

output :

  0
  hello
  1
  hello
  2
  3
  hello
  4
  hello
  
if you can see, in the example program is not printing the hello string when i == 2. but continues with the loops next iteration. 

Infinity Loop

you can initiate infinite loop as below.
    package main

    import (
        "fmt"
    )

    func main() {
    	i := 1
        for {
            fmt.Println(i)
        }

    }

Collection Loop

you can iterate any collection using normal loop. but go has a very minimalistic way of looping a collection where you can iterate collection in a simple way which is range.
  package main
  import ("fmt")

  func main() {
      array := []string{"hello","world","!!!"}
      marks := map[string]int{"Bangla":80,"English":90,"Science":95}

      for i,v := range array {
          fmt.Println(i,v)
      }

      for i,v := range marks {
          fmt.Println(i,v)
      }
  }  

output :

  0 hello
  1 world
  2 !!!
  Bangla 80
  English 90
  Science 95
  
if you can see in the example. range loop is assigning index and value in variable i and v and printing it.

Switch

Switch on golang is parity much self explanatory. you can understand switch by the example below.
  package main
  package main

  import (
      "fmt"
  )

  func main() {
      nameAgeList := map[string]int{"Alax":18,"Jack":10,"Dani":21}

      for name,age := range nameAgeList {
          switch name {
          case "Alax" :
              fmt.Println("go to collage your age is",age)

          case "Jack" :
              fmt.Println("go to school your age is",age)

          case "Dani" :
              fmt.Println("go to University your age is",age)	
          }

      } 

  }

output :

  go to collage your age is 18
  go to school your age is 10
  go to University your age is 21

Panic

Panic is used in golang where program hits a position that it doesn't knows what to do. i know you can use error and exceptions in go for that but in panic it will terminate the program and report where it hits the problem.
  package main

  import (
      "fmt"
  )

  func main() {
      nameAgeList := map[string]int{"Alax":18,"Jack":19,"Dani":21}

      for name,age := range nameAgeList {
          switch name {
          case "Alax" :
              fmt.Println("go to collage your age is",age)

          case "Jack" :
              if age > 18 {
                  panic("somthing is worng")
              }
              fmt.Println("go to school your age is",age)

          case "Dani" :
              fmt.Println("go to University your age is",age)	
          }

      } 

  }

output :

    go to collage your age is 18
    panic: somthing is worng

    goroutine 1 [running]:
    main.main()
        /tmp/sandbox467193843/prog.go:17 +0x3d1
  












Comments

Popular posts from this blog

Functions and Methods of golang

  Overview In this blog i am going to talk about the functions and methods in golang. which is a important topic in go. if you have not read the previous blog or do not have a basic knowledge about go the i would suggest you to have a look at my other blog.  Functions   Methods Functions you can declare a function as below. you don't need to declare return type unless your not returning something from your function. you get the return value in a way that is explained in the example code . func <function name > ( <parameter> <parameter type> ) ( <return type> ){} Example Code : package main import ("fmt") func main() { test1() // example 1 sum := test2() // example 2 fmt.Println("sum =",sum) sum2,error:= test3(30,40) // example 3 fmt.Println("sum 2 =",sum2,error) sum3,_:= test3(80,60) // example 4 fmt.Println("sum 2 =",sum3) su...

List of data types in Golang

  index Description 1 Type: int8 8-bit signed integer 2 Type:  int16 16-bit signed integer 3 Type:  int32 32-bit signed integer 4 Type:  int64 64-bit signed integer 5 Type:  uint8 8-bit unsigned integer 6 Type:  uint16 16-bit unsigned integer 7 Type:  uint32 32-bit unsigned integer 8 Type:  uint64 64-bit unsigned integer 9 Type:  int Both in and uint contain same size, either 32 or 64 bit. 10   Type:  uint Both in and uint contain same size, either 32 or 64 bit. 11 Type:  rune It is a synonym of int32 and also represent Unicode code points. 12 Type:  byte It is a synonym of int8 . 13 ...

Golang Commands

Overview    In this blog I am going to explain about basic go command and in depth knowledge about go commands will come after this series of basic go programming end. fallowing topic will be explained in this blog : primary commands help topic Commands Go ahead open command prompt on type go and press Enter. you can see all the commands and help topic that go provide. Primary Commands   this all are primary commands that go provide. this all are self explanatory. we have "bug" command for bug report, "clean" to clean previous projects, "install" to install packages to project.mod command needs explanation. which we will discuss on the next blog. but the command I want to talk about is doc. if we run ''go doc <package name>'' command  example "go doc json.decoder.decode" it will show the out put below. and if you run "go doc json.decoder" it will show the out put below. and if you run "go doc json" ...