Conditions with if

Contents
The if statement
if-else chains
If with initializer
Guard clauses

The if statement

The if statement runs a block only when its condition is true, and the condition needs no parentheses.

Keep conditions simple and put the opening brace on the same line, exactly as the compiler requires.

package main import "fmt" func main() { age := 20 if age >= 18 { fmt.Println("adult") } if age < 18 { fmt.Println("minor") } }

go run .

adult

if-else chains

When several outcomes exclude each other, chain if with else if and finish with else for the fallback case.

Only the first block with a true condition runs, so order the checks from the most specific to the most general one.

package main import "fmt" func main() { score := 85 if score >= 90 { fmt.Println("excellent") } else if score >= 70 { fmt.Println("good") } else { fmt.Println("needs work") } }

go run .

good

If with initializer

An if statement can declare a short lived variable before the condition, separated by a semicolon.

The variable exists only inside the if and else blocks, which keeps the outer scope clean.

package main import "fmt" func main() { if n := 7 % 2; n == 0 { fmt.Println("even") } else { fmt.Println("odd") } }

go run .

odd

Guard clauses

A guard clause checks bad input first and returns early, so the main logic below stays flat and easy to read.

Prefer several small guards over one deeply nested condition whenever a function validates its arguments.

package main import "fmt" func check(age int) string { if age < 0 { return "age cannot be negative" } if age < 18 { return "minor" } return "adult" } func main() { fmt.Println(check(20)) fmt.Println(check(10)) fmt.Println(check(-1)) }

go run .

adult
minor
age cannot be negative

Next: switch

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: