golang tutorial - Golang Global Variables | Golang Var | Go Global Variable - golang - go programming language



Variable Definition in Go:

  • Variable is the name given to a memory location to store a value of a specific type.

Below is the syntax of variable declaration

var variable_list optional_data_type;
  • Here, optional_data_type is a valid Go data type including
    • byte,
    • int,
    • float32,
    • complex64,
    • boolean or any user-defined object, etc.,
 if statement
  • variable_list may consist of one or more variables or identifier names separated by commas. Some valid declarations are shown here:
var    a, b, c int;  
var   x, ch byte;
var  f, average float32;
d = 42;
  • var a,b,c are of integer type
  • var x and ch is of type byte.
  • Variables can be initialized (assigned an initial value) in their declaration. The type of variable is automatically judged by the compiler based on the value passed to it. The initializer consists of an equal sign followed by a constant expression as follows:
variable_name = value;

Some examples are:

d = 42;    // definition of d is not done. Here d is integer type. 
  • Variables declared without a corresponding initialization are zero-valued. For example, the zero value for an int is 0.
  • var declares 1 or more variables.
  • The := syntax is shorthand for declaring and initializing a variable, For e.g. var f string = "short" in this case.
package main

import "fmt"

func main() {

    var a string = "First"
    fmt.Println(a)

    var b, c int = 1, 2
    fmt.Println(b, c)

    var d = true
    fmt.Println(d)

    var e int
    fmt.Println(e)

    f := "Last"
    fmt.Println(f)
}

Output :

$ go run variables.go
First
1 2
true 
0
Last

Static type declaration

  • A static type variable declaration will have the declaration clearly.
  • A variable declaration has its meaning at the time of compilation only, compiler needs actual variable declaration at the time of linking the program.

Example

package main

import "fmt"

func main() {
   var a float64
   a = 90.0
   fmt.Println(a)
   fmt.Printf("a is of type %T\n", x)
}

Output :

90
a is of type float64

Dynamic type declaration / Type Inference

  • A dynamic type variable declaration requires compiler to interpret the type of variable based on value passed to it. Compiler don't need a variable to have type statically as a necessary requirement.

Example

  • Try the following example, where variables have been declared without any type, and have been defined and initialized inside the main function. Notice, in case of type inference, we've initialized the variable y with := operator where as x is initilized using = operator.
package main

import "fmt"

func main() {
   var x float64 =90.0

   y := 14 
   fmt.Println(x)
   fmt.Println(y)
   fmt.Printf("x is of type %T\n", x)
   fmt.Printf("y is of type %T\n", y)	
}

Output :

90
14
x is of type float64
y is of type int

Mixed variable declaration

  • Variables of different types can be declared at a shot as below,
   var a, b, c = 3, 4, "wikitechy"  

Example

package main

import "fmt"

func main() {
   var a, b, c = 3, 4, "wikitechy"  
	
   fmt.Println(a)
   fmt.Println(b)
   fmt.Println(c)
   fmt.Printf("a is of type %T\n", a)
   fmt.Printf("b is of type %T\n", b)
   fmt.Printf("c is of type %T\n", c)
}

Output:

3
4
wikitechy 
a is of type int
b is of type int
c is of type string


Related Searches to Golang Global Variables | Golang Var | Go Global Variable