If/else, switch, anonymous function and type assertion in golang

Malka AliMalka Ali
3 min read

# πŸ‘‹ Hey, I'm Malka!

Welcome to **Day 3** of my **#100DaysOfDevelopment** journey.

Today, I explored two fundamental concepts in programming β€” `if/else` and `switch` statements in **Go (Golang)**.

Let’s dive in! πŸš€

---

## πŸ” What I Learned

### 🧠 `if`, `else if`, `else` β€” The Decision-Maker

Go’s `if/else` structure is clean and expressive.

```go

package main

import "fmt"

func main() {

age := 18

if age < 18 {

fmt.Println("You are a minor.")

} else if age == 18 {

fmt.Println("Just turned adult!")

} else {

fmt.Println("You're an adult.")

}

}

πŸ“Œ Key Points:

No need for parentheses () around the condition.

Curly braces {} are mandatory, even for a single line.

---

✨ Bonus: Short Statement in if

Go lets you declare and use a variable inside an if statement:

if score := 90; score > 80 {

fmt.Println("Great job!")

}

🧠 score is scoped only inside the if block β€” this keeps the outer scope clean!

---

πŸ”„ switch β€” A Cleaner Alternative

When handling multiple conditions, switch makes code cleaner than chaining if/else.

day := 3

switch day {

case 1:

fmt.Println("Monday")

case 2:

fmt.Println("Tuesday")

case 3:

fmt.Println("Wednesday")

default:

fmt.Println("Another day")

}

πŸ“Œ Key Points:

No need for break β€” Go adds it automatically.

Use fallthrough if you want execution to continue to the next case.

You can match multiple values like this:

switch day {

case 6, 7:

fmt.Println("Weekend!")

}

---

🎁 Bonus Concept: Anonymous Functions, Interfaces, and Type Assertion in Switch

Here’s something cool I discovered β€” defining a function without a name (aka an anonymous function) and storing it in a variable:

whatAmI := func(i interface{}) {

switch t := i.(type) {

case bool:

fmt.Println("I'm a bool")

case int:

fmt.Println("I'm an int")

default:

fmt.Printf("Don't know type %T\n", t)

}

}

whatAmI(true)

whatAmI(1)

whatAmI("hey")

πŸ“˜ Explanation:

whatAmI is a variable holding an anonymous function.

i interface{} means the function can accept any type.

Inside, we use switch t := i.(type) β€” this is called a type switch.

\> 🧠 This special i.(type) syntax only works inside a type switch.

To assert a single type manually, you'd use: t := i.(int)

βœ… Very useful when dealing with dynamic or unknown data types!

---

πŸ€“ Mini Challenge I Tried

I built a program that checks whether a number is positive, negative, or zero:

num := -5

if num > 0 {

fmt.Println("Positive")

} else if num < 0 {

fmt.Println("Negative")

} else {

fmt.Println("Zero")

}

🌟 Practicing this sharpened my logic muscles and boosted my confidence!

---

🌱 Takeaway

Control flow statements like if/else and switch are at the heart of logic-building in any programming language.

They help our programs make decisions, making them smart, responsive, and dynamic.

---

πŸ‘©β€πŸ’» I’m learning from Go by Example and documenting my journey to stay accountable and help other beginners like me.

Let’s grow together πŸ’ͺ

---

#100DaysOfCode | #GoLang | #DevJourney | #ifelseInGolang | #SwitchInGolang|#anonymousFunctionInGolang|#TypeAssertionInGolang|#LearningInPublic | #MalkaCodes

---

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!