Friday 3 May 2019

Go language: if statement: combine initialisation and condition evaluation in same line


Syntax
if initialization; condition {

}

Example
a := 10

if a == 10 {
         fmt.Println("Value of a is 10")
}

Above statements can be written like below.

if a := 10; a == 10 {
         fmt.Println("Value of a is 10")
}

HelloWorld.go
package main

import "fmt"

func main() {

 if a := 10; a == 10 {
  fmt.Println("Value of a is 10")
 }else {
  fmt.Println("Value of a is 11")
 }
}

Output
Value of a is 10



Previous                                                 Next                                                 Home

No comments:

Post a Comment