Back to blog
GoLang 101: Mastering Variables, Types, and Memory in Go

GoLang 101: Mastering Variables, Types, and Memory in Go

Kazem

Kazem

Jul 17, 2025 · 3 min read

#go#programming#webdev

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:

Here’s a simple example:

var x int
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Declaring and Initializing Variables

There are a few ways to create and assign values to variables in Go.

  1. Declare and Assign Separately
var x int
x = 10
Enter fullscreen mode Exit fullscreen mode
  1. Declare and Assign Together
var x int = 10
Enter fullscreen mode Exit fullscreen mode
  1. Let Go Infer the Type
var x = 10  // Go will assume x is an int
Enter fullscreen mode Exit fullscreen mode
  1. Use Short Declaration (Inside Functions Only)
x := 10
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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:

You can even define your own aliases for clarity:

type Celsius float64
type UserID int

var temp Celsius = 36.5
var id UserID = 123
Enter fullscreen mode Exit fullscreen mode

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:

Example:

x := 5
p := &x      // p points to x
fmt.Println(*p)  // prints 5
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Variable Scope

Scope refers to where in your code a variable can be accessed.

In Go, scope is block-based and hierarchical. That means:

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
Enter fullscreen mode Exit fullscreen mode

Stack vs. Heap

When you run a Go program, memory is divided into two major areas:

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)
}
Enter fullscreen mode Exit fullscreen mode

In this article we talked about:


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!

GoLang 101: Mastering Variables, Types, and Memory in Go - Kazem