Switch case in Go

Hi everyone, I’m new with programming and I started studying GO.
I don’t understand waht’s wrong with my code in the following case, why is it not working?:

package main

import (
“fmt”
“math/rand”
“time”
)

func main() {
rand.Seed(time.Now().UnixNano())
cani := rand.Intn(200)
fmt.Println("ho avuto: ", cani, “cani”)

switch cani {
case >=10 :
fmt.Println(cani, “ah però”)
case >=50 :
fmt.Println(cani, “uomo vero”)
case >=100 :
fmt.Println(cani, “sei un dio”)
default:
fmt.Println(cani, “meglio di nulla”)
}

}

The syntax of a switch statement is:

switch (expression) {
    case ...

The expression is evaluated and then the result of that evaluation is compared to the cases. If one of the values matches, the code for that case is executed.

You have switch cani {...
That is fine. cani will evaluate to some integer value.
But in your cases, you have >= 10, >= 50, >= 100
That is not correct. There are a few problems with the cases given your choice of cani as the switch expression.
Consider the example:

switch cani {
   case 32:
         // some code
    case 86:
         // some code

In the above snippet, cani will evaluate to an integer value. That integer value will be checked against other integer values such as 32 and 86. If cani matches one of these integers exactly, then the code for that case will run (If the switch expression evaluated to a string, then the cases should also be strings).
Now consider the example,

switch cani {
   case >= 10:
   ...

What is >= 10 supposed to mean? The switch statement doesn’t know that you want to evaluate cani >= 10
Suppose you edited the above to:

switch cani {
   case cani >= 10:
   ...

This will also not work. cani is some integer. But cani >= 10 is a comparison which will evaluate to true/false (boolean). Comparing integer to boolean value causes a type mismatch.

You want your cases to make some comparisons. That is fine. But once the comparison is made, the case will evaluate to a boolean (true/false) value. Therefore, the switch expression should also evaluate to a boolean.
This syntax will work:

switch true {
   case cani >= 10:
   ...

If one of the cases evaluates to true, it will match the true value of the switch expression and the code for that case will execute. In Golang, we can omit the true

switch {
   case cani >= 10:
   ...

This will also work.

Other than the syntax, you also need to think about the logic of your cases.
The cases are checked from top to bottom.
If case cani >= 10 is your first case and cani is some integer greater than or equal to 10, then the code for this case will run and the other cases will be skipped.

What you want to do is:

switch  {
	case (cani >= 100):
		fmt.Println(cani, "sei un dio")
	case (cani >= 50):
		fmt.Println(cani, "uomo vero")
	case (cani >= 10):
		fmt.Println(cani, "ah però")
	default:
		fmt.Println(cani, "meglio di nulla")
	}

Now, if cani is greater/equal to 100, then the first case will run. If cani is between 50 and 100, then the second case will run. If cani is between 10 and 50, then the third case will run. If cani is less than 10, then the default will run.
If you reverse the order of the cases, then the cani >= 10 case will match when cani is greater/equal to 10. If it is less than 10, then the default will run. You will never be able to reach the cani >= 50 and cani >= 100 cases. Play around with some numbers and you will understand why the order of cases is important.

If you want to see different variations of switch statements, have a look at the following (in the link, “The conditional case statement” is similar to what you are trying to do):

Thank you very much, now it works :slight_smile: