Introduction to Variables and Data Types
Every program needs to store some data/information in memory. The data is stored in memory at a particular memory location.
A variable is just a convenient name given to a memory location where the data is stored. Apart from a name, every variable also has an associated type.
Data Types or simply Types, categorize related set of data, define the way they are stored, the range of values they can hold, and the operations that can be done on them.
For example, Golang has a data type called int8
. It represents 8-bit integers whose values can range from -128 to 127. It also defines the operations that can be done on int8
data type such as addition, subtraction, multiplication, division etc.
We also have an int
data type in Golang whose size is machine dependent. It is 32 bits wide on a 32-bit system and 64 bits wide on a 64-bit system.
Other examples of data types in Golang are bool
, string
, float32
, float64
etc. You’ll learn more about these data types in the next tutorial. I gave a brief idea of data types here because it is necessary to understand them before we dive deep into Golang variables.
Golang Variables in depth
Declaring Variables
In Golang, We use the var
keyword to declare variables -
var firstName string
var lastName string
var age int
You can also declare multiple variables at once like so -
var (
firstName string
lastName string
age int
)
You can even combine multiple variable declarations of the same type with comma -
var (
firstName, lastName string
age int
)
Zero values
Any variable declared without an initial value will have a zero-value depending on the type of the variable-
Type | Zero Value |
---|---|
bool | false |
string | "" |
int, int8, int16 etc. | 0 |
float32, float64 | 0.0 |
The example below demonstrates the concept of zero values:
package main
import "fmt"
func main() {
var (
firstName, lastName string
age int
salary float64
isConfirmed bool
)
fmt.Printf("firstName: %s, lastName: %s, age: %d, salary: %f, isConfirmed: %t\n",
firstName, lastName, age, salary, isConfirmed)
}
# Output
firstName: , lastName: , age: 0, salary: 0.000000, isConfirmed: false
Declaring Variables with initial Value
Here is how you can initialize variables during declaration -
var firstName string = "Satoshi"
var lastName string = "Nakamoto"
var age int = 35
You can also use multiple declarations like this -
var (
firstName string = "Satoshi"
lastName string = "Nakamoto"
age int = 35
)
Or even combine multiple variable declarations of the same type with comma and initialize them like so -
var (
firstName, lastName string = "Satoshi", "Nakamoto"
age int = 35
)
Type inference
Although Go is a Statically typed language, It doesn’t require you to explicitly specify the type of every variable you declare.
When you declare a variable with an initial value, Golang automatically infers the type of the variable from the value on the right-hand side. So you need not specify the type when you’re initializing the variable at the time of declaration -
package main
import "fmt"
func main() {
var name = "Rajeev Singh" // Type declaration is optional here.
fmt.Printf("Variable 'name' is of type %T\n", name)
}
# Output
Variable 'name' is of type string
In the above example, Golang automatically infers the type of the variable as string
from the value on the right-hand side. If you try to reassign the variable to a value of some other type, then the compiler will throw an error -
var name = "Rajeev Singh" // Type inferred as `string`
name = 1234 // Compiler Error
Type inference allows us to declare and initialize multiple variables of different data types in a single line like so -
package main
import "fmt"
func main() {
// Multiple variable declarations with inferred types
var firstName, lastName, age, salary = "John", "Maxwell", 28, 50000.0
fmt.Printf("firstName: %T, lastName: %T, age: %T, salary: %T\n",
firstName, lastName, age, salary)
}
# Output
firstName: string, lastName: string, age: int, salary: float64
Short Declaration
Go provides a short variable declaration syntax using :=
operator. It is a shorthand for declaring and initializing a variable (with inferred type).
For example, the shorthand for var name = "Rajeev"
is name := "Rajeev"
. Here is a complete example -
package main
import "fmt"
func main() {
// Short variable declaration syntax
name := "Rajeev Singh"
age, salary, isProgrammer := 35, 50000.0, true
fmt.Println(name, age, salary, isProgrammer)
}
# Output
Rajeev Singh 35 50000 true
Note that, a Short variable declaration can only be used inside a function. Outside a function, every statement needs to begin with a keyword like var
, func
etc, and therefore, :=
operator is not available.
Conclusion
Variables are one of the basic concepts in any programming language. In this article, you learned how to declare variables in Golang, how type inference works, and how to use multiple declarations and short declarations.
As always, Thanks for reading.
Next Article: Golang Basic Types, Operators, and Type Conversion
Code Samples: github.com/callicoder/golang-tutorials