Golang tutorials - Variables starter guide with examples
This blog post, It covers the Golang Tutorials variables guide with examples.
Golang variables
Variables
usage in go language is the same as in another programming language such as java.
Variables are the name given to a space in memory where values are stored. These are various types of values that are stored. Based on each type, memory size will be accommodated.
Variable names can be named using letters
, digits
, and underscore
characters. Because the Go language is case-sensitive, upper and lower case names are different.
Variables
in go language are declared using the var
keyword.
It must first be declared before you can use a variable
.
How to declare Variable in Golang?
Variables
can be declared using the var
keyword. Once variable
is declared, the compiler allocates memory based on the type of the variable
.
The variable
declaration contains three parts
- var keywords
- variable name
- type of variable
var variablename(s) variableType
It can be single or multiple, variable types are predefined inbuilt types or custom types.
Inbuilt types can be byte
, int
and float32
, and string
.
Following are a various way of variable declaration examples
initialized variables
It can be declared and initialized with values.
It can be done in many ways. The following example explains converting different types.
package main
import (
"fmt"
)
func main() {
m: = 42
n: = float64(2.0)
var o float64 = float64(25.2)
fmt.Println(m)
fmt.Println(n)
fmt.Println(o)
}
Output:
42
2
25.2
Multiple variable declarations
Here multiple variables declare in a single line of the same type.
package main
import (
"fmt"
)
func main() {
var a, b, c int;
var (
m = 1 n = 20 o = 3
)
}
Short variable declarations
variables can be declared and assigned with initial values. Multiple variables declare in a single line and readability is improved and code lines reduced.
package main
import "fmt"
func main() {
var m, n int = 10,
20
fmt.Println(m, n) // 10 20
}
How to declare Global Scope variables in golang?
variables
declared outside function blocks and at the package level.
This variable does access across all functions declared in the program. These can exist as long as the program is running.
package main
import "fmt"
//Global Variables Declaration
var globalVariable = "Hello";
func main() {
fmt.Println(globalVariable);
}
And output of the above code is
Hello
How to define Local Scope variables?
Variables declared inside a function or block of code is called a local variable
. The variables do not access outside the function.
package main
import "fmt"
func main() {
//Local Variables Declaration
var localVariable = "this is test variable";
fmt.Println(localVariable);
}
And output of the above code is
this is test variable
Variables default values in golang
These also can be assigned with a default value with values zero for number types such as Int
, Float
, etc.. false
for Boolean
types, empty string -” for Strings
.
package main
import "fmt"
func main() {
var intVariable int32
var floatVariable float32
var booleanVariable bool
var strVariable string
fmt.Printf("%v %v %v %q\n", intVariable, floatVariable, booleanVariable, strVariable) // output 0 0 false ""
}
Golang type inference
type inference
is called When a variable declares without the type of the variable. The type can derive type from the right-hand side of the value.
inference
can be applied to variables and constants.
Below example descript using variable expression using = and:= operators.
package main
import "fmt"
func main() {
var k int
l: = k
var m = 42
var n = false
p: = 3.2 // constant
fmt.Printf("variable k datatype is %T\n", k)
fmt.Printf("variable l datatype is %T\n", l)
fmt.Printf("variable m datatype is %T\n", m)
fmt.Printf("variable n datatype is %T\n", n)
fmt.Printf("variable p datatype is %T\n", p)
}
The output of the above program code is
variable k datatype is int
variable l datatype is int
variable m datatype is int
variable n datatype is bool
variable p datatype is float64
How to declare Constants in golang
Constants
are like variables that can not be changed value, once the value is assigned. Constants
can be declared using :=
syntax. const
can be applied to all types of data such as boolean, strings, and numeric types
package main
import "fmt"
func main() {
const str = "kiran"
fmt.Println("Hi", str) // Hi kiran
const booleanValue = true
fmt.Println(booleanValue) // true
}
The difference between variable and constant
, Constant values are fixed and variables are dynamic. Constant throws an exception if you tried to change its value. Variables change their value at any time.
Let’s see some examples using golang variables.
How to find a memory or address location of a variable?
This example gets variable pointer reference memory in golang.
To get memory location, variables in golang can be referenced using & the variable name - ampersand variable.
- & the variable is a pointer of a variable which prints the memory location of the variable
- *& the variable is a value of a variable at memory location
This example prints the pointer of a variable
package main
import "fmt"
func main() {
var m int
fmt.Println("memory location for m :", & m, "variable value is ", * & m)
n: = 15
fmt.Println("memory location for n :", & n, "variable value is ", * & n)
}
The output of the above code is
memory location for m : 0x416020 variable value is 0
memory location for n : 0x416024 variable value is 15
Conclusion
In this tutorial, learn how to declare variables and the scope of variables, define fixed values constants and type inference
An example for finding a memory or address location of a variable