Variables in Go Language

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

Working with Variables in Go lang

 

In Go (or Golang), working with variables is fundamental to storing and manipulating data within programs. Variables in Go are containers that hold values of a specific type, such as integers, strings, booleans, or custom types defined by the programmer.

Go requires variables to be declared with a specific type before they can be used. Once declared, variables can be assigned values, which can be updated or modified as needed during program execution. Go supports various primitive data types like int, float64, string, bool, as well as complex types such as structs, arrays, slices, maps, and pointers.

Go variables have scope, which determines where they can be accessed within the program. Variables can be defined at different levels of scope, such as package-level, function-level, or block-level scope. The scope of a variable influences its visibility and lifetime.

Understanding how to declare, initialize, and use variables effectively is crucial for writing efficient and readable Go code. By mastering variable usage in Go, developers can efficiently manage data and build robust, scalable applications that leverage the power of Go’s statically typed system and concurrency features.

  • 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 :

    PS C:\GO_Language\Variable> go run var1.go
    Prwatech
    323
    true
    

     

  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 :

    PS C:\GO_Language\Variable> go run variables.go
    Global Variable : 50
    Pakage Variable : 70
    value of a : 50
    value of b : 44
    value of c : 35
    Addition of a & b : 94
    
  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 :

    PS C:\GO_Language\Variable> go run variables.go
    Global Variable : 50
    Pakage Variable : 70
    value of a : 50
    value of b : 44
    value of c : 35
    Addition of a & b : 94
    

     

  4. Program to illustrate variables using a loop.
    package main
    import "fmt"
    
    func main() {
        var Prwa = "Google"
        fmt.Println("Normal String:")
        fmt.Println("%s", Prwa)
        fmt.Println("hex bytes \n")
        for i := 0; i < len(Prwa); i++ {
            fmt.Println("%x", Prwa[i], "\n")
        }
    }
    

    Output :

    PS C:\GO_Language\Variable> go run var3.go
    Normal String: %s  Google
    hex bytes 
    
     %x  71 
    
     %x  111 
    
     %x  111 
    
     %x  103 
    
     %x  108 
    
     %x  101
    

     

  5. Program to illustrate mixed variables.
    package main
    import "fmt"
    
    func main() {
        var x, y, z = 5, 4.5, "Prwatech" 	//Mixed variable declaration.
        fmt.Println("The value of X :", x)
        fmt.Println("The value of Y:", y)
        fmt.Println("The value of Z :", z)
    }
    

    Output :

    PS C:\GO_Language\Variable> go run var4.go
    The value of X : 5
    The value of Y: 4.5
    The value of Z : Prwatech
    

    Working with Variables in Go lang

0
0

Quick Support

image image