"Understand variables, how they live in RAM, Go’s core data types, declaration styles, reassignment rules, and constants."
When learning Go (Golang), understanding variables and data types is essential.
They form the foundation of every program — acting as the bridge between logic and memory.
In this guide, we’ll explore
- The concept of variables
- How memory (RAM) works with variables
- Go’s primary data types
- Different variable declaration methods
- Reassignment rules and type immutability
- Constants in Go
Let’s dive in 👇
The Concept of Variables
A variable is simply a container — a vessel, bucket, or pot (বালতি, গামলা, বাসন) used to store something.
Just like you might store coffee in a mug or rice in a dish, a program stores data in variables.
- Purpose: To store and manage data — the heart of programming and computer science.
-
Naming: Programmers often use Var (V-A-R) as shorthand for "Variable."
They also shorten other terms, like writing
int
instead of “Integer.”
Variables and Computer Memory
When you declare a variable, it doesn’t just exist in code — it occupies space in your computer’s RAM (memory).
Components of a Computer Relevant to Variables
Component | Function / Role |
---|---|
CPU (Central Processing Unit) | Executes instructions. |
RAM (Random Access Memory) | Temporarily stores variables and data during program execution. |
Hard Disk | Long-term data storage (not directly used for variables at runtime). |
How Variables Occupy RAM
When you declare something like a := 10
, Go asks the computer to allocate space for it in memory:
- RAM is divided into many small storage spaces called cells.
- Declaring a variable occupies one of those empty cells.
- That cell is given a name (e.g.,
a
). - The value (e.g.,
10
) is stored inside that cell.
So a := 10
means:
“Hey computer, take one cell in RAM, label it a, and put 10 inside it.”
Data Types (ডাটা টাইপ)
Every variable stores data — but not all data is the same type.
Go’s data types define what kind of value a variable can hold.
Table of Primary Data Types in Go
Category | Meaning / Example | Go Type | Description |
---|---|---|---|
Numeric | Numbers of any kind. | — | Divided into integers and floating-point types. |
Integer (পূর্ণ সংখ্যা) | Whole numbers (e.g., 10 , -5 ). |
int , int8 , int16 , int32 , int64 , uint , uint8 , etc. |
Whole numbers. The suffix (8, 16, 32, 64) refers to bit size. |
Floating Point (দশমিক সংখ্যা) | Decimal numbers (e.g., 10.5 , 40.34 ). |
float32 , float64
|
For numbers with decimals. |
Boolean (বুলিয়ান) | True or False values. | bool |
Can only be true or false . |
String (স্ট্রিং) | Words or text. | string |
Enclosed in double quotes, e.g., "Hello World" . |
Note: More complex types (like arrays, slices, and structs) come later — start simple!
Declaring Variables in Go
Go offers several ways to declare variables.
Knowing one or two is enough to start coding efficiently.
A. Explicit Declaration (Type Specified)
You tell Go exactly what type the variable will hold:
var x int = 10
-
var
→ tells Go you’re declaring a variable. -
x
→ the variable name. -
int
→ its data type. -
= 10
→ the assigned value.
B. Type Inference (Let Go Decide)
Go is smart — it can infer the data type automatically based on the value.
Example 1 — Long Form
var a = 10 // Go infers: a is int
Example 2 — Short-Hand Declaration
a := 10 // infers int
pi := 3.14 // infers float64
name := "Go" // infers string
isCool := true // infers bool
The
:=
operator both declares and initializes a variable in one step.
Reassigning Variables
Variables are “containers,” so their contents can change.
Rules for Reassignment
- You cannot use
:=
again — that’s only for declaring. - To update, use the simple
=
operator.
a := 100
a = 50 // ✅ OK (reassignment)
If you try this:
a := 100
a := 50 // ❌ Error: no new variables on left side
Go will complain — you’re redeclaring instead of reassigning.
Type Immutability
Once a variable is assigned a type, it cannot hold a value of a different type.
flag := true
flag = false // ✅ OK
flag = "Habib" // ❌ Error: cannot assign string to bool
Variables in Go are “loyal” — they stick to their original type.
Constants (স্থির মান)
A constant (const
) holds a value that never changes — like a permanent variable.
const Pi = 3.1416
- Declared using
const
. - Cannot be reassigned — doing so triggers a compiler error:
const P = 100
P = 200 // ❌ Error: cannot assign to P
Constants are great for fixed values like configuration keys, mathematical constants, or version numbers.
Conclusion
- Variables are at the core of Go programming — they link your logic to memory.
- They occupy RAM cells and store data with a defined type.
- Go offers smart type inference and multiple declaration styles.
- Variable types are fixed after declaration, ensuring type safety.
- Use constants for unchangeable values.
Happy Go-ing!
Top comments (0)