Go - Variables

Variables can be declared in multiple ways in Go
  1. using var {} -- here we declared using var keyword with parenthesis and group multiple variables together
    • var {

                              varialbe1 <type_of_variable> = variable1_value

                              Nam1 string = "World" 

      }

       

  2. var var1, var2 <type_of_var> = var1_value, var2_value  --two variables are declared here using "var" keyword and in this approach we can declare any number of variables and all variables must be of same type
    • For e.g., var var1, var2 string = "local1", "local2"
  3. var var_name <type_of_var> -- this is declaration of variable without value
    • var var_name <type_of_var> -- this is declaration of variable without value
    • var name1 string
    • var isNumber bool or var isNumber bool = true
  4. temp


String Variables

Integer Variables

Boolean Variables

Constants - declared using const keyword and can't be changed once initialized. Constants are scoped depending on where they are declared and must be assigned a value at the time of declaration.

No comments:

Post a Comment