So far, we've talked about how data is stored in memory and the different data types available in Python and Go. Storing data is only part of programming. At some point, our programs need to make decisions.
When thinking about real-world applications, you can think:
- Should a user be allowed to log in?
- Is a student eligible for admission?
- Has a customer completed payment?
- Should an email notification be sent?
In all these situations, the program needs to decide what action to take based on certain conditions. This is where conditionals come in. Conditionals allow a program to make decisions based on whether a condition is true or false.
Although Python and Go solve the same problem, they approach conditionals in slightly different ways. In this article, we’ll explore both.
The Syntax Shift: How Python and Go Define Code Blocks
One of the first things you'll notice when writing conditionals in Python and Go is that they define code blocks differently.
In Python, Indentation Matters
Python uses a combination of colons (:) and indentation to indicate which code belongs to a condition.
age = 18
if age >= 18:
print("You are an adult")
Notice two things:
- The condition ends with a colon (:).
- The code inside the condition is indented.
The indentation is not optional.
For example:
age = 18
if age >= 18:
print("You are an adult")
This produces an error because Python relies on indentation to understand the structure of your program. Python's philosophy here is simple:
Code should be readable and visually organized.
By forcing indentation, Python ensures that developers write code in a consistent format.
In Go, Curly Braces Define Structure
Go takes a different route. Instead of indentation, it uses curly braces {} to define blocks of code.
age := 18
if age >= 18 {
fmt.Println("You are an adult")
}
The braces tell Go exactly where the conditional block starts and ends. Even if you change the spacing or indentation, Go still understands the structure because the braces define it. One important thing to note is that Go does not require parentheses around conditions.
if age >= 18 {
fmt.Println("Adult")
}
Not:
if (age >= 18) {
fmt.Println("Adult")
}
While the second style is common in some languages, Go intentionally keeps the syntax cleaner.
If/Else Chains
A single condition is useful, but real applications often need multiple possible outcomes.
For example:
- A score above 70 is an A.
- A score above 60 is a B.
- A score above 50 is a C.
- Anything lower is a fail.
This is where if/else chains become useful.
Python's elif
Python introduces the elif keyword, which stands for "else if."
score = 75
if score >= 70:
print("Grade A")
elif score >= 60:
print("Grade B")
elif score >= 50:
print("Grade C")
else:
print("Fail")
The program checks each condition from top to bottom. As soon as one condition evaluates to True, Python executes that block and skips the rest. This keeps the code concise and easy to read.
Go's else if
Go achieves the same thing using else if.
score := 75
if score >= 70 {
fmt.Println("Grade A")
} else if score >= 60 {
fmt.Println("Grade B")
} else if score >= 50 {
fmt.Println("Grade C")
} else {
fmt.Println("Fail")
}
Functionally, this behaves exactly like Python's elif. The main difference is syntax. Python creates a special keyword (elif). Go combines two existing keywords (else if).
Quick Comparison
| Python | Go |
|---|---|
| if | if |
| elif | else if |
| else | else |
| Uses indentation | Uses braces |
Pattern Matching and Multi-Way Decisions
Sometimes an application needs to compare a value against many possible options. Imagine you're building a system that handles user roles:
- Admin
- Staff
- Student
- Guest
Using multiple if statements can become messy. Both Python and Go provide a cleaner solution, but they do it differently.
Python's match/case
Starting from Python 3.10, Python introduced match/case.
role = "Admin"
match role:
case "Admin":
print("Full access")
case "Staff":
print("Limited access")
case "Student":
print("Student access")
case _:
print("Guest access")
Think of match as saying:
"Compare this value against several patterns and execute the matching block."
The underscore (_) acts as a default case. One reason match/case is powerful is that it can do more than simple value comparisons.
It can match:
- Values
- Lists
- Tuples
- Object structures
- Complex patterns
This makes it much more powerful than traditional switch statements found in many languages.
Go's switch Statement
Go uses switch.
role := "Admin"
switch role {
case "Admin":
fmt.Println("Full access")
case "Staff":
fmt.Println("Limited access")
case "Student":
fmt.Println("Student access")
default:
fmt.Println("Guest access")
}
At first glance, this looks similar to Python's match/case. However, Go's switch statement has a few interesting capabilities.
Switch Without an Expression
One unique feature of Go is that a switch can be used without directly switching on a variable.
score := 75
switch {
case score >= 70:
fmt.Println("Grade A")
case score >= 60:
fmt.Println("Grade B")
default:
fmt.Println("Fail")
}
This behaves almost like an organized replacement for a long if/else if chain.
Multiple Values in a Case
Go also allows multiple matching values.
day := "Saturday"
switch day {
case "Saturday", "Sunday":
fmt.Println("Weekend")
default:
fmt.Println("Weekday")
}
This can make decision logic much cleaner.
Conclusion: Python's Angle vs Go's Angle
Python's Angle
Python focuses on readability and expressiveness.
- Indentation clearly shows structure.
- elif makes multi-condition chains easy to read.
- match/case supports powerful pattern matching.
- The language tries to make decision logic feel natural and close to plain English.
Go's Angle
Go focuses on simplicity and predictability.
- Curly braces explicitly define blocks.
- else if keeps the language small by reusing existing keywords.
- The switch is versatile enough to replace many if/else chains.
- The language avoids adding too many specialized features.
Thanks for reading. Hope you learned something new. Read, like and comment, thanks.
Top comments (0)