Ver en Español
Conditionals in Go
Apr 15, 2023
Updated: Jun 25, 2026

Conditionals in Go

Conditionals in Go are a way to control the flow of a program. You can use if, else, else if and switch to decide which instructions run based on certain conditions. Let's look at each structure step by step.

If, Else and Else If

The "if / else if" conditionals are a way to make decisions in programming. They work like this:

  • You start with an if that evaluates a condition. If the condition is true, the block of code associated with that if runs.
  • If the first if condition is false, control moves to the next else if (if there is one). Its condition is evaluated and, if it is true, the block associated with that else if runs.
  • This process repeats for each else if in sequence until a true condition is found or until there are no more else if branches. When a true condition is found, its block runs and we exit the conditional structure.
  • If none of the if or else if conditions is true, the else block runs (if present), which is the last resort.

Let's look at a first example, a conditional that evaluates whether a variable is greater than the number 10:

package main

import "fmt"

func main() {

	var x int = 11
	if x > 10 {
		fmt.Println("x es mayor a 10")
	} else {
		fmt.Println("x no es mayor a 10")
	}

}

Notice the message in the else: since this branch runs whenever the condition x > 10 is false, it also covers the case where x is exactly 10. That is why we say "x is not greater than 10" rather than "x is less than 10", which would leave the value 10 out.

Unlike other languages, in Go conditionals do not require parentheses around the condition being evaluated. On the other hand, the opening brace { is mandatory and must sit on the same line as the condition. If you put it on the next line, the code will not compile.

Now let's look at an if / else if example, where I can add as many cases as I want using the else if syntax:

func ejemploIfElseIf() {
	var x int = 11
	if x > 10 {
		fmt.Println("x es mayor a 10")
	} else if x == 10 {
		fmt.Println("x es igual a 10")
	} else {
		fmt.Println("x es menor a 10")
	}
}

Switch in Go

The switch structure is a flow control mechanism that lets you simplify multiple comparisons of a variable against different values. It works like this:

  • Start of switch: the structure begins with the switch keyword, followed by the variable or expression you want to compare.
  • Cases (case): inside the switch block you define different cases with the case keyword, followed by a specific value you want to compare against the switch variable or expression. If the variable or expression matches the case value, the block of code associated with that case runs.
  • Running a case: when a match is found between the variable and a case, the code for that case runs and then we exit the switch structure. This means only the code of one matching case runs.
  • Break: although some languages require the break keyword at the end of each case block to stop control from flowing into the next one, in modern languages like Go the break is implicit and you do not need to write it. If you want the opposite behavior, that is, for execution to continue into the next case, you can explicitly use the fallthrough keyword.
  • default: you can include a default at the end of the switch, which runs if none of the cases match. It is similar to an else in an if / else structure.

Example of switch:

func ejemploSwitch() {
	var x int = 11
	switch x {
	case 1:
		fmt.Println("x es 1")
	case 2:
		fmt.Println("x es 2")
	case 3:
		fmt.Println("x es 3")
	case 4:
		fmt.Println("x es 4")
	case 5:
		fmt.Println("x es 5")
	case 6:
		fmt.Println("x es 6")
	case 7:
		fmt.Println("x es 7")
	case 8:
		fmt.Println("x es 8")
	case 9:
		fmt.Println("x es 9")
	case 10:
		fmt.Println("x es 10")
	default:
		fmt.Println("x es mayor a 10")
	}
}

Example of switch / case with ranges:

func ejemploSwitchRango() {
	var numeroMes int = 3
	var estacion string

	switch {
	case numeroMes >= 1 && numeroMes < 3:
		estacion = "Invierno"
	case numeroMes >= 3 && numeroMes < 6:
		estacion = "Primavera"
	case numeroMes >= 6 && numeroMes < 9:
		estacion = "Verano"
	case numeroMes >= 9 && numeroMes < 12:
		estacion = "Otoño"
	default:
		estacion = "Ninguna estación del año!"
	}
	fmt.Println(estacion)
}

Example of switch / case with strings:

func ejemploSwitchConTexto() {
	var x string = "def"
	// Evaluate the value of variable 'x' in a 'switch' structure
	switch x {
	// If 'x' equals "abc"
	case "abc":
		// Print the message "x es abc" to the console
		fmt.Println("x es abc")
	case "def":
		// Print the message "x es def" to the console
		fmt.Println("x es def")
	// If 'x' is neither "abc" nor "def"
	default:
		// Print the message "x es otra cadena" to the console
		fmt.Println("x es otra cadena")
	}
}

Proposed exercises

Practice with these Go conditional exercises and send your solution as a Pull Request to this repository: Go Para Principiantes.

Note: verify that the "Go Para Principiantes" repository URL is correct and still published before promoting the post. verify

10 conditional exercises in Go:

  1. Print a different message depending on whether a number is even or odd.
  2. Given two integers, print the larger of the two.
  3. Given three integers, print the largest of the three.
  4. Print a different message depending on whether a number is positive or negative.
  5. Read an integer and determine whether it is divisible by 5.
  6. Read an integer and determine whether it is divisible by 3 and 5 at the same time.
  7. Read two integers and determine whether the first is greater than the second.
  8. Read three integers and determine whether the first equals the second and the third.
  9. Read two integers and determine whether the first is less than the second or they are equal.
  10. Read an integer and determine whether it is even or odd and whether it is positive or negative.

10 switch exercises in Go:

  1. Print a different message depending on whether a number is between 0 and 5, between 5 and 10, between 10 and 20, or greater than 20.
  2. Given two integers, print the letter corresponding to the appropriate grade.
  3. Given three integers, print the letter corresponding to the appropriate grade.
  4. Read an integer and determine what kind of number it is.
  5. Read an integer between 0 and 5 and determine which letter it corresponds to.
  6. Read an integer between 5 and 10 and determine which letter it corresponds to.
  7. Read an integer between 10 and 20 and determine which letter it corresponds to.
  8. Read an integer greater than 20 and determine which letter it corresponds to.
  9. Read an integer and determine whether it is even or odd and whether it is positive or negative.
  10. Read an integer and determine whether it is divisible by 2, 3, 4, 5 or 6.

3-point summary

  1. Conditionals in Go (if, else, else if) let you control the flow of a program based on one or more conditions, with no parentheses around the condition but with the opening brace mandatory on the same line.
  2. The switch statement in Go makes it easy to compare a variable against multiple expressions and runs the block associated with the match; the break is implicit and fallthrough lets you continue into the next case when you need it.
  3. Practicing with conditional and switch exercises in Go helps you improve the flow control of your programs and better understand how to make decisions based on conditions and expressions.

Now that you have learned about conditionals and switch in Go, it is time to put your skills into practice. Work on the exercises above and send your solutions as Pull Requests to the Go Para Principiantes repository.

That's all, I hope this post is useful to you and that you can apply it to a project you have in mind. Leave me a comment if it helped or if you have any questions, and remember that if you liked it you can also share it using the social links below. Good luck and have fun programming in Go.

Sebastian Gomez

Sebastian Gomez

Creador de contenido principalmente acerca de tecnología.

Leave a Reply

0 Comments

Advertisements

Related Posts

Categorias