Conditionals¶
if/else, ternary expressions, match/case, and truthy/falsy rules.
if / elif / else¶
Ternary¶
match / case¶
switch command {
case "quit":
os.Exit(0)
case "hello", "hi":
fmt.Println("Hello!")
default:
if strings.HasPrefix(command, "/") {
fmt.Printf("Command: %s\n", command)
} else {
fmt.Println("Unknown")
}
}
Go switch does not fall through by default
Unlike C/Java, Go's switch breaks automatically after each case.
Use fallthrough explicitly if needed.
Truthy / Falsy¶
// Go requires explicit bool — no truthy/falsy
if len(myList) > 0 { // must compare explicitly
process(myList)
}
if name != "" { // cannot just use: if name {
fmt.Println(name)
}
Go conditions must be bool
You cannot use if x { where x is an int, string, or pointer. Go requires an explicit boolean expression.