A Beginner's Guide to Variables and Loops in Go

---
# Hey, I'm Malka! ๐
Welcome to **Day 2** of my **#100DaysOfDevelopment** journey. Today, Iโm diving into **Variables and Loops in Go (Golang)**.
I'm currently learning from [**Go by Example**](https://gobyexample.com/) โ an excellent hands-on resource for beginners!
---
## ๐ง Understanding Variables in Go
We all know that a **variable** is like a container that stores data. But Go brings its own twist. Here's what Iโve learned so far:
---
### โ 1. **Basic Declaration**
Go uses the `var` keyword to declare variables:
```go
var a string
You can also declare multiple variables of the same type on a single line:
var a, b int
---
๐งฉ 2. Type Inference
Go can infer the variable type from the value you assign:
var a = "hello"
Here, you donโt need to mention string โ Go automatically figures it out.
\> โ ๏ธ You must assign a value in this case, otherwise the compiler will throw an error.
---
โก 3. Shorthand Declaration
Go provides a short and clean way to declare and initialize variables using :=:
f := "apple"
fmt.Println(f)
No need for var
No need to specify the type
Must initialize at the same time
\> ๐ This shorthand doesn't work for constants or without initialization.
---
๐ 4. Declaring Variables of Different Types
You canโt do this:
// โ Invalid
var a int, b string
But Go allows multiple declarations with inferred types:
// โ Valid
var a, b = 10, "Malka"
Or even shorter:
a, b := 10, "Malka"
---
๐ Constants in Go
Constants are declared using the const keyword:
const a = 7
If a value is provided, Go will infer the type.
Constants cannot be changed after theyโre declared.
You cannot use := for constants.
---
๐ Loops in Go
Go has only one loop keyword โ for. But itโs super flexible!
---
๐น 1. Standard For Loop
Similar to C/C++, but Go-style:
for i := 0; i < 5; i++ {
fmt.Println(i)
}
---
๐ธ 2. While-Like Loop
i := 0
for i <= 10 {
fmt.Println(i)
i++
}
---
๐ 3. Infinite Loop
for {
fmt.Println("I am an infinite loop")
}
To stop it, press Ctrl + C or use a break statement:
for {
fmt.Println("Only once")
break
}
---
๐ 4. Looping a Fixed Number of Times (Using range)
for i := range 3 {
fmt.Println(i)
}
This acts like a loop from 0 to 2 โ useful for repeating tasks a fixed number of times.
---
๐ฏ What I Learned Today
โ Variable declaration and initialization
โ Shorthand syntax using :=
โ Constants in Go
โ Flexible use of the for loop
โ How Go syntax differs from C/C++
---
๐ Whatโs Next?
On Day 3, Iโll explore if/else statements in Go โ diving into decision-making logic.
Iโm also brainstorming some unique beginner-friendly project ideas. I want to go beyond basic calculators or to-do apps and build something useful, creative, and different.
๐ก Got a cool idea? Drop it in the comments or send me a DM โ Iโd love to connect and collaborate!
---
๐ฌ Letโs Connect!
I'm documenting my journey in simple, beginner-friendly language to help others learn alongside me.
Follow along, and letโs grow together on this path of development ๐
Thanks for reading!
Subscribe to my newsletter
Read articles from Malka Ali directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Malka Ali
Malka Ali
๐ Hi, Iโm Malka โ a curious and dedicated BCA student exploring the world of backend development. Iโm currently learning Go and diving deep into APIs, DevOps, and Cloud technologies. I write to document my learning journey, simplify complex concepts, and grow with the developer community. Iโm passionate about building real-world projects and contributing to open-source in the future. ๐ Open to internships,full time working, freelancing work, tech collaborations, and mentorship!