Welcome back! 🎉 In the last article, we talked about why Go is worth learning and how to get started. Now it’s time to dive into something every programming language needs: variables and data types. We’ll also talk about memory in Go—how variables are stored, scoped, and cleaned up with Go’s built-in garbage collector.
Let’s get into it!
What Is a Variable?
A variable is just a name for a chunk of memory that holds some data—like a number, a string, or something more complex.
In Go, every variable must have:
- A name (like x or userAge)
- A type (like int, string, or float64)
Here’s a simple example:
var x int
This tells Go: “Please reserve a space in memory called x to store an integer.”
You can also declare multiple variables at once:
var x, y int
Declaring and Initializing Variables
There are a few ways to create and assign values to variables in Go.
- Declare and Assign Separately
var x int
x = 10
- Declare and Assign Together
var x int = 10
- Let Go Infer the Type
var x = 10 // Go will assume x is an int
- Use Short Declaration (Inside Functions Only)
x := 10
This one is super common in Go. It’s quick and clean, but it only works inside functions.
Constants
Constants are like variables—but their value never changes. They’re great for fixed values like mathematical constants or configuration settings.
const pi = 3.14159
const appName string = "MyGoApp"
You can’t use := for constants. Also, constant values must be assigned at compile time.
Types in Go
Go is statically typed, meaning the type of each variable is known at compile time. This helps catch bugs early and improves performance.
Basic types:
- int, int64, uint – whole numbers
- float32, float64 – decimal numbers
- string – a sequence of characters
- bool – true or false
You can even define your own aliases for clarity:
type Celsius float64
type UserID int
var temp Celsius = 36.5
var id UserID = 123
This doesn’t change the actual type (Celsius is still a float64), but it adds semantic meaning, which helps when reading or maintaining code.
Memory and Pointers
Every variable lives somewhere in memory. Go lets you work with pointers, which store memory addresses instead of actual values.
Two pointer operators you need to know:
- &x gives you the address of x
- *p gives you the value at address p
Example:
x := 5
p := &x // p points to x
fmt.Println(*p) // prints 5
You can also create new pointers like this:
ptr := new(int) // ptr is a pointer to an int with a zero value
*ptr = 100 // set the value at the pointer
Variable Scope
Scope refers to where in your code a variable can be accessed.
In Go, scope is block-based and hierarchical. That means:
- A variable declared outside of functions is accessible everywhere
- A variable declared inside a function (or block) is local to that function/block
var x = 10 // global
func printX() {
fmt.Println(x) // OK, x is visible here
}
func anotherFunc() {
var y = 20
fmt.Println(y) // OK here
}
fmt.Println(y) // Error: y is out of scope here
Stack vs. Heap
When you run a Go program, memory is divided into two major areas:
- Stack – short-term memory used for function calls and local variables. It’s fast and automatically cleaned up.
- Heap – long-term memory for data that might live longer than a function call. It requires garbage collection.
In most low-level languages like C, you’d have to manually manage this. But…
Go Has Garbage Collection
Garbage collection means Go automatically frees up memory that’s no longer in use. This is a huge win for developers—it reduces bugs, prevents memory leaks, and simplifies your code.
Example:
func foo() *int {
x := 10
return &x
}
func main() {
ptr := foo()
fmt.Println(*ptr)
}
In this article we talked about:
- Use var or := to declare variables
- Go types include int, float64, string, bool, and more
- Pointers let you work with memory addresses
- Scopes define where your variables live and how long they stick around
- Go’s garbage collector handles cleanup for you, making your life easier
In the next article, we’ll start working with control structures like if, for, and switch, and start writing real logic in Go.
Until then—happy coding!
Top comments (0)