Variables and Data Types in Go
Variables and Data Types
Hey there! In this guide, we'll explore the concepts of variables and data types in Go. Variables are essential in programming, allowing you to store and manipulate data dynamically. Let's dive in!
Video Explanationโ

1. Introduction to Variablesโ
In Go, variables are explicitly declared and used by the compiler to e.g. check type-correctness of function calls. Go is statically typed, meaning once a variable is declared with a specific type, it cannot change to hold another type.
2. Variable Declarationโ
You can declare a variable using the var keyword, followed by the variable name and its type.
var name string = "Alice"
var age int = 25
var isStudent bool = true
Zero Valuesโ
If you declare a variable without giving it an initial value, Go automatically assigns it a zero value.
0for numeric typesfalsefor booleans""(empty string) for strings
var count int // count is automatically 0
3. Short Variable Declarationsโ
Inside a function, you can use the := short assignment statement in place of a var declaration with implicit type. This is the most common way to declare variables in Go!
func main() {
name := "Bob" // Go infers this is a string
age := 30 // Go infers this is an int
height := 5.9 // Go infers this is a float64
fmt.Println(name, age, height)
}
Note: Short declarations (:=) can only be used inside functions.