An 'if' statement in Go checks whether a condition is true or false. If the condition is true, the code inside the curly braces `{}` is executed. You don't need to use parentheses around the condition, but you can if you want to.
healthy := true if healthy { fmt.Println("Go to work.") }
An 'else' statement is used after an 'if' statement to specify a block of code that runs if the 'if' condition is false.
sick := true if sick { fmt.Println("Stay home.") } else { fmt.Println("Go to work.") }
Comparison operators in Go are used to compare two values. They return a boolean value (`true` or `false`). Common comparison operators include `==` (equal to), `!=` (not equal to), `<` (less than), `<=` (less than or equal to), `>` (greater than), and `>=` (greater than or equal to).
if 5 > 3 { fmt.Println("5 is greater than 3.") }
Logical operators in Go are used to combine multiple boolean expressions or to negate a boolean expression. The common logical operators are `&&` (logical AND), `||` (logical OR), and `!` (logical NOT).
sunny := true warm := false if sunny && !warm { fmt.Println("It's sunny but not warm.") } else { fmt.Println("It's either not sunny or it's warm.") }
An 'else if' statement allows you to check multiple conditions after the initial 'if'. If the 'if' condition is false, Go checks the 'else if' condition. If that's true, the associated block of code is executed.
temperature := 70 if temperature < 60 { fmt.Println("Wear a jacket.") } else if temperature < 75 { fmt.Println("Wear a sweater.") } else { fmt.Println("Wear light clothes.") }
Go allows you to declare and initialize a variable in the same statement where you use it in an 'if' or 'switch' condition. This is done using `:=` to declare a new variable.
if num := 5; num > 0 { fmt.Println("Positive number.") }
A 'switch' statement in Go allows you to compare a single expression against multiple values. It's an alternative to using multiple 'if' and 'else if' statements. The code for the first matching case is executed.
day := "Monday" switch day { case "Monday": fmt.Println("Start of the work week.") case "Friday": fmt.Println("End of the work week.") default: fmt.Println("It's a regular day.") }
To generate random numbers in Go, you need to set a 'seed' value. The seed initializes the random number generator. By default, Go uses the same seed each time, so you need to set it with a unique value (like the current time) to get different results.
import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) fmt.Println(rand.Intn(100)) // Prints a random number between 0 and 99 }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!