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!
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