Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
In programming, decision-making structures allow you to execute specific blocks of code based on certain conditions. Go supports several decision-making statements, and this tutorial will guide you through them.
if
StatementThe if
statement is the most fundamental decision-making statement.
package main import "fmt" func main() { x := 20 if x > 10 { fmt.Println("x is greater than 10.") } }
if-else
StatementYou can use the else
statement to specify a block of code to be executed if the condition in the if
statement is false.
x := 5 if x > 10 { fmt.Println("x is greater than 10.") } else { fmt.Println("x is less than or equal to 10.") }
if-else if-else
ChainFor multiple conditions, you can chain if-else
statements.
x := 15 if x > 20 { fmt.Println("x is greater than 20.") } else if x > 10 { fmt.Println("x is greater than 10 but less than or equal to 20.") } else { fmt.Println("x is less than or equal to 10.") }
switch
StatementThe switch
statement is used to select one of many blocks of code to be executed.
y := 2 switch y { case 1: fmt.Println("y is 1.") case 2: fmt.Println("y is 2.") default: fmt.Println("y is neither 1 nor 2.") }
switch
Statement with Multiple ExpressionsA case can have multiple expressions.
y := 5 switch y { case 1, 3, 5, 7: fmt.Println("y is an odd number less than 10.") case 2, 4, 6, 8: fmt.Println("y is an even number less than 10.") default: fmt.Println("y is 10 or greater.") }
switch
Statement with InitializationYou can have an optional initialization statement in the switch
.
switch z := 5 + 3; z { case 8: fmt.Println("5 + 3 equals 8.") case 9: fmt.Println("5 + 3 equals 9.") }
switch
Statement without an ExpressionA switch
without an expression is an alternate way to express if/else logic.
x := 42 switch { case x%2 == 0: fmt.Println("x is even.") default: fmt.Println("x is odd.") }
select
StatementThough not precisely a standard decision-making statement, the select
statement lets a Go program wait for multiple communication operations. It's used with channels, and it chooses the case that can proceed, making it useful for decision-making in concurrent programming.
ch1 := make(chan string) ch2 := make(chan string) go func() { ch1 <- "From Channel 1" }() go func() { ch2 <- "From Channel 2" }() select { case msg1 := <-ch1: fmt.Println(msg1) case msg2 := <-ch2: fmt.Println(msg2) }
This tutorial covered the essential decision-making statements in Go. Mastering these structures will allow you to write dynamic programs that can respond and adjust to different situations.
If Statements in Golang with Examples:
if
statement in Golang is used for conditional execution.package main import "fmt" func main() { num := 10 // If statement if num > 0 { fmt.Println("Number is positive") } }
Conditional Statements in Golang Programming:
if
, else if
, and else
.package main import "fmt" func main() { num := 0 // Conditional statements if num > 0 { fmt.Println("Number is positive") } else if num < 0 { fmt.Println("Number is negative") } else { fmt.Println("Number is zero") } }
Golang if-else Statements and Usage:
if-else
statements handle multiple conditions.package main import "fmt" func main() { num := 10 // If-else statement if num%2 == 0 { fmt.Println("Number is even") } else { fmt.Println("Number is odd") } }
Comparisons and Logical Operators in Golang Conditions:
package main import "fmt" func main() { age := 25 // Comparisons and logical operators if age >= 18 && age <= 60 { fmt.Println("Age is between 18 and 60") } }
Using Switch-Case for Multiple Conditions in Golang:
switch
statements handle multiple conditions with case
.package main import "fmt" func main() { day := "Saturday" // Switch-case statement switch day { case "Monday": fmt.Println("It's Monday!") case "Saturday", "Sunday": fmt.Println("It's the weekend!") default: fmt.Println("It's a regular day") } }
Golang Nested If Statements and Their Structure:
if
statements are used for deeper conditional checks.package main import "fmt" func main() { num := 10 // Nested if statements if num > 0 { if num%2 == 0 { fmt.Println("Number is positive and even") } else { fmt.Println("Number is positive and odd") } } else { fmt.Println("Number is not positive") } }
Ternary Operators in Golang for Decision Making:
package main import "fmt" func main() { num := 10 // Conditional expression as a substitute for ternary operator result := func() int { if num > 0 { return 1 } return 0 }() fmt.Println("Result:", result) }