Variables and Data Types
Variables
The var statement declares one or more variables. Like function parameters, the type is specified at the end.
It can be used at both the package level and within functions.
package main
import "fmt"
var golang, python, c bool
func main() {
var rust int
fmt.Println(rust, golang, python, c)
}
Variables with Initializers
A var declaration can include initializers, one per variable.
If an initializer is provided, the type can be omitted. In such cases, the variable’s type is inferred from the initializer.
package main
import "fmt"
var i, j int = 1, 2
func main() {
var golang, python, rust = true, false, "yes"
fmt.Println(i, j, golang, python, rust)
}
Short variable declarations
When you are inside a function, you can use a shortcut := to declare and assign a variable all at once.
Go is smart enough to automatically infer the data type.
package main
import "fmt"
func main() {
var i, j int = 1, 2
k := 3
golang, python, rust := true, false, "yes"
fmt.Println(i, j, k, golang, python, rust)
}
var or func.
As a result, the := short variable declaration syntax is not allowed at the package level.Zero values
var keyword but don't assign a value right away (like var name int), Go will safely set it to a "zero value" (which is 0 for integers) rather than leaving it undefined.The zero value is:
0for numeric types,falsefor the boolean type, and""(the empty string) for strings.
package main
import "fmt"
func main() {
var i int
var f float64
var b bool
var s string
fmt.Printf("%v %v %v %q\n", i, f, b, s)
}
The Core Data Types
As a beginner, you will mostly work with these four basic types:
string: For text (e.g., "Hello")int: For whole numbers (e.g., 10)float64: For decimal numbers (e.g., 10.5)bool: For true/false values (e.g., true)
package main
import "fmt"
func main() {
// 1. The Standard Way: Explicitly stating the type
var name string = "Alice"
var age int = 25
// 2. The Short Way: Go infers the type automatically
height := 5.8 // inferred as float64
isStudent := true // inferred as bool
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Height:", height)
fmt.Println("Is Student:", isStudent)
}
Type Conversions
In Go, type conversion is explicit. You must clearly specify when converting a value from one type to another.
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
Type Inference
In Go, you don’t always need to explicitly specify a variable’s type. When a variable is declared with an initializer, Go determines its type automatically based on the assigned value.
This applies when using both:
- short declaration syntax (
:=) varwith an initializer
var i int
j := i // j gets the same type as i (int)
If the assigned value already has a defined type, the new variable adopts that type.
Untyped Constants Behavior
When the assigned value is an untyped constant, Go decides the type based on the value’s form and precision.
i := 42 // inferred as int
f := 3.142 // inferred as float64
g := 0.867 + 0.5i // inferred as complex128
Common Doubts
f := 3.142 // f is float64
f = 8 // works fine
Even though 8 looks like an integer, it is an untyped constant. Go automatically adapts it to float64 to match f.
So this is effectively treated as:
f = 8.0
Only when the variable is already declared as float64. The 8 adapts to whatever type the variable holds.
f := 3.142 // f is float64
f = 8 // 8 adapts to float64 - fine
var x float32 = 8 // 8 adapts to float32 - also fine
But if you create a typed int value and try to assign it:
f := 3.142
n := 8 // n is now a typed int (inferred)
f = n // cannot use n (int) as float64
8 (bare literal) is untyped and flexible. n := 8 locks n to int - now it's typed and strict.