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

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