<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Day 3 of learning development]]></title><description><![CDATA[Day 3 of learning development]]></description><link>https://malka-day3-of-learning-development.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 23 Jun 2026 21:25:01 GMT</lastBuildDate><atom:link href="https://malka-day3-of-learning-development.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[If/else, switch, anonymous function and type assertion in golang]]></title><description><![CDATA[# 👋 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...]]></description><link>https://malka-day3-of-learning-development.hashnode.dev/ifelse-switch-anonymous-function-and-type-assertion-in-golang</link><guid isPermaLink="true">https://malka-day3-of-learning-development.hashnode.dev/ifelse-switch-anonymous-function-and-type-assertion-in-golang</guid><category><![CDATA[if-else]]></category><category><![CDATA[anonymous function]]></category><category><![CDATA[type assertions]]></category><category><![CDATA[golang]]></category><category><![CDATA[Switch case]]></category><dc:creator><![CDATA[Malka Ali]]></dc:creator><pubDate>Thu, 26 Jun 2025 06:15:46 GMT</pubDate><content:encoded><![CDATA[<p># 👋 Hey, I'm Malka!</p>
<p>Welcome to **Day 3** of my **#100DaysOfDevelopment** journey.</p>
<p>Today, I explored two fundamental concepts in programming — `if/else` and `switch` statements in **Go (Golang)**.</p>
<p>Let’s dive in! 🚀</p>
<p>---</p>
<p>## 🔍 What I Learned</p>
<p>### 🧠 `if`, `else if`, `else` — The Decision-Maker</p>
<p>Go’s `if/else` structure is clean and expressive.</p>
<p>```go</p>
<p>package main</p>
<p>import "fmt"</p>
<p>func main() {</p>
<p>age := 18</p>
<p>if age &lt; 18 {</p>
<p>fmt.Println("You are a minor.")</p>
<p>} else if age == 18 {</p>
<p>fmt.Println("Just turned adult!")</p>
<p>} else {</p>
<p>fmt.Println("You're an adult.")</p>
<p>}</p>
<p>}</p>
<p>📌 Key Points:</p>
<p>No need for parentheses () around the condition.</p>
<p>Curly braces {} are mandatory, even for a single line.</p>
<p>---</p>
<p>✨ Bonus: Short Statement in if</p>
<p>Go lets you declare and use a variable inside an if statement:</p>
<p>if score := 90; score &gt; 80 {</p>
<p>fmt.Println("Great job!")</p>
<p>}</p>
<p>🧠 score is scoped only inside the if block — this keeps the outer scope clean!</p>
<p>---</p>
<p>🔄 switch — A Cleaner Alternative</p>
<p>When handling multiple conditions, switch makes code cleaner than chaining if/else.</p>
<p>day := 3</p>
<p>switch day {</p>
<p>case 1:</p>
<p>fmt.Println("Monday")</p>
<p>case 2:</p>
<p>fmt.Println("Tuesday")</p>
<p>case 3:</p>
<p>fmt.Println("Wednesday")</p>
<p>default:</p>
<p>fmt.Println("Another day")</p>
<p>}</p>
<p>📌 Key Points:</p>
<p>No need for break — Go adds it automatically.</p>
<p>Use fallthrough if you want execution to continue to the next case.</p>
<p>You can match multiple values like this:</p>
<p>switch day {</p>
<p>case 6, 7:</p>
<p>fmt.Println("Weekend!")</p>
<p>}</p>
<p>---</p>
<p>🎁 Bonus Concept: Anonymous Functions, Interfaces, and Type Assertion in Switch</p>
<p>Here’s something cool I discovered — defining a function without a name (aka an anonymous function) and storing it in a variable:</p>
<p>whatAmI := func(i interface{}) {</p>
<p>switch t := i.(type) {</p>
<p>case bool:</p>
<p>fmt.Println("I'm a bool")</p>
<p>case int:</p>
<p>fmt.Println("I'm an int")</p>
<p>default:</p>
<p>fmt.Printf("Don't know type %T\n", t)</p>
<p>}</p>
<p>}</p>
<p>whatAmI(true)</p>
<p>whatAmI(1)</p>
<p>whatAmI("hey")</p>
<p>📘 Explanation:</p>
<p>whatAmI is a variable holding an anonymous function.</p>
<p>i interface{} means the function can accept any type.</p>
<p>Inside, we use switch t := i.(type) — this is called a type switch.</p>
<p>\&gt; 🧠 This special i.(type) syntax only works inside a type switch.</p>
<p>To assert a single type manually, you'd use: t := i.(int)</p>
<p>✅ Very useful when dealing with dynamic or unknown data types!</p>
<p>---</p>
<p>🤓 Mini Challenge I Tried</p>
<p>I built a program that checks whether a number is positive, negative, or zero:</p>
<p>num := -5</p>
<p>if num &gt; 0 {</p>
<p>fmt.Println("Positive")</p>
<p>} else if num &lt; 0 {</p>
<p>fmt.Println("Negative")</p>
<p>} else {</p>
<p>fmt.Println("Zero")</p>
<p>}</p>
<p>🌟 Practicing this sharpened my logic muscles and boosted my confidence!</p>
<p>---</p>
<p>🌱 Takeaway</p>
<p>Control flow statements like if/else and switch are at the heart of logic-building in any programming language.</p>
<p>They help our programs make decisions, making them smart, responsive, and dynamic.</p>
<p>---</p>
<p>👩‍💻 I’m learning from Go by Example and documenting my journey to stay accountable and help other beginners like me.</p>
<p>Let’s grow together 💪</p>
<p>---</p>
<p>#100DaysOfCode | #GoLang | #DevJourney | #ifelseInGolang | #SwitchInGolang|#anonymousFunctionInGolang|#TypeAssertionInGolang|#LearningInPublic | #MalkaCodes</p>
<p>---</p>
]]></content:encoded></item></channel></rss>