i {
case i%3 == 0 && i%5==0:fmt.Println("FizzBuzz")
case i%3 == 0:fmt.Println("Fizz")
case i%5 == 0:fmt.Println("Buzz")
default: fmt.Println(i)
}
errors :
/main.go:39:3: cannot use i % 3 == 0 && i % 5 == 0 (type bool) as type int
./main.go:40:3: cannot use i % 3 == 0 (type untyped bool) as type int
./main.go:41:3: cannot use i % 5 == 0 (type untyped bool) as type int
hey guys , i've just started learning go a week ago . what is the problem with this code ?
You can't assign condition to case statement, if you want to give condition then use if - else
aha . so in case we want to check for multiple conditions in our code we should use if , else if , else but in case that we wanna do something base on the value of a variable , we can use swich case thanks man
Yp exactly, here's an example also if you want.
play.golang.org
of course you can: https://play.golang.org/p/xacceowwwJv
wow . thanks Roman . the only difference is : we should not pass the variable to the switch statement . so if we pass a variable name to the switch statement , then we should just compare the value of that variable ( we can't evaluate some condition ) but , if we don't pass any variable to switch , then we can evaluate as much as condition .
Aah... Never knew this existed, looks interesting but as far as readability goes I would preffer using if else for conditionals ig...
Обсуждают сегодня