Похожие чаты

For i :=1 ; i<100 ; i++ { switch

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 ?

8 ответов

11 просмотров

You can't assign condition to case statement, if you want to give condition then use if - else

"Mahdi"- Автор вопроса
DΛNTΣ
You can't assign condition to case statement, if y...

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

"Mahdi"
aha . so in case we want to check for multiple con...

Yp exactly, here's an example also if you want.

play.golang.org

DΛNTΣ
You can't assign condition to case statement, if y...

of course you can: https://play.golang.org/p/xacceowwwJv

"Mahdi"- Автор вопроса
Roman Sharkov
of course you can: https://play.golang.org/p/xacce...

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 .

Roman Sharkov
of course you can: https://play.golang.org/p/xacce...

Aah... Never knew this existed, looks interesting but as far as readability goes I would preffer using if else for conditionals ig...

Похожие вопросы

Обсуждают сегодня

Карта сайта