A Beginner's Guide to Variables and Loops in Go

Malka AliMalka Ali
3 min read

---

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

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