Variables in Go Language

  • date 4th August, 2022 |
  • by Prwatech |
  • 0 Comments
  • Types of variable

  1. Int
  2. Float32
  3. String
  4. Bool

 In Go language variables are defined in two different ways:

  1. Using var keyword

 Syntax:

Var variable_name type =value

 

  1. Using the short variable declaration

 Syntax:

              Variable_name :=value

 

  1. Simple variable deceleration Go Lang program.
    package main
    import "fmt"
    
    func main() {
        var a string = "Prwatech"
        var b int = 323
        var c bool = true
    
        fmt.Println(a)
        fmt.Println(b)
        fmt.Println(c)
    }
    

    Output :

  2. Program to illustrate local variable and global variable.
    package main
    import "fmt"
    //global -- (Pascal Case)
    var Val2 int = 50
    //Package Level --(Camel Case)
    var myValue int = 70
    
    func main() {
        println("Global Variable :", Val2)
        println("Pakage Variable :", myValue)
    
        var a int //variable declearation    <-- Local Variable
        a = 50    //intilazation
        fmt.Println("value of a :", a)
        var b int = 44 // variable declearation with intilazation
        fmt.Println("value of b :", b)
        var c = 35
        fmt.Println("value of c :", c)
        fmt.Println("Addition of a & b :", a+b)
    }
    

    Output :

  3. Program to illustrate variable using function.
    package main
    import "fmt"
    
    var x = 15
    
    func Prwa() {
        var x = 6
        fmt.Println("in Prwa :", x)
    }
    func main() {
        Prwa()
        fmt.Println("in Main :", x)
    
    }
    

    Output :

Quick Support

image image