A variable is a symbolic name given to the storage location which contains some value that can be changed at any time during the execution of the program. A variable must be defined with the type of data or value it is holding.
There are several data types in Go.
bool int uint float32 complex64 string int8 uint8 float64 complex128 byte int16 uint16 rune int32 uint32 error int64 uint64
In some programming languages variable holds a
undefined value when not initialized, Go gives it a zero value of its data type. A
boolean variable if not initialized, get the
false value and an
integer variable gets
string variable will get
"" (empty) value.
Declaring a variable
var the statement declares a list of variables. The type is at the last of the statement.
var varName dataType
var i, j, k int var f float32 var u uint
Variables with initializers
We can initialize variables while declaring.
var varName dataType = value
var i int = 10 var s string = "hello" var ok bool = true
We can omit the type of variable, it will take the type of initializers
var i = 42 // int var f = 3.142 // float64 var g = "hello" // string
This is the most commonly used variable declaration inside functions.
varName := value
i := 10 s := "hello" ok := true
When declaring a variable without specifying it’s type the variable’s type is inferred from the value on the right-hand side.
var i int j := i // j is an int i := 42 // int f := 3.142 // float64 g := 0.867 + 0.5i // complex128
Assign or Re-assign
You can assign or re-assign value to a declared variable as:
varName = value
i = 12 s = "world" ok = false
Multiple variable declarations
Multiple variables of the same type with the single var statement
var i, j, k int var x, y, z = 0.867 + 0.5i, 4.65, true
Multiple variables of the different types with the single var statement
var ( i int = 10 s string = "hello" ok bool = true )
Go supports constants of character, string, boolean, and numeric values. Constants are declared using the
const str string = "constants"
Untyped and Typed
Constants can be declared with or without a type in Go. If we are declaring a literal constant, then we are declaring constants that are untyped and unnamed.
const str string = "constants" //Typed constant const i = 10 //Untyped constant, literal declaration const f = 3.14
The constants on the LHS of the declaration are named constants and the literal values on the RHS are unnamed constants.
Typed constants don’t use the same type system as variables, they've their implementation for representing the values that we associate with them.
In the case of a typed constant, the declared type is used to associate the type’s precision limitations or kind of constant.
In the case of an untyped constant, the literal value will determine what kind/type the constant takes
Every GO compiler has the flexibility to implement constants as they wish, within the mandatory set of requirements.
Thank you for reading this blog, and please give your feedback in the comment section below.
Did you find this article valuable?
Support Pratik Jagrut by becoming a sponsor. Any amount is appreciated!