how to use a `switch` statement in Go

huangapple go评论85阅读模式
英文:

how to use a `switch` statement in Go

问题

package main

import "fmt"

func main() {
	var age int
	fmt.Scanf("%d", &age)

	// 在这里编写你的 switch 或者 if...else-if 语句。

	switch age {
	case 1:
		age <= 14
		fmt.Println("Toy Story 4")
	case 2:
		age <= 18
		fmt.Println("The Matrix")
	case 3:
		age <= 25
		fmt.Println("John Wick")
	case 4:
		age <= 35
		fmt.Println("Constantine")
	case 5:
		age > 35
		fmt.Println("Speed")
	default:
		fmt.Println("Error: Unknown age")
	}
}

我在IDE中的scanf处得到了一个黄色标记,标记出一个未处理的错误。此外,所有的case语句的第一行都被标记为红色,导致无法编译。我得到的错误是age <= 14被评估,但未被使用。对于所有的语句都是如此。我在网上搜索并查看了示例,从我所看到的材料和示例中,代码是正确的。这是来自 Go Land (JetBrains) 的屏幕截图:

有人有什么想法吗?另外,如果你回答,请记住,这是一个 Go 课程的一部分,我必须使用 scanf。我可以将 switch 改为 else if,这可能会解决问题,但我有点好奇为什么会发生这种情况,以及如何解决它。 how to use a `switch` statement in Go

英文:
package main

import &quot;fmt&quot;

func main() {

	var age int
	fmt.Scanf(&quot;%d&quot;, &amp;age)

	// Code your switch or if...else-if statement here.
	
    switch age {
	case 1:
		age &lt;= 14
		fmt.Println(&quot;Toy Story 4&quot;)
	case 2:
		age &lt;= 18
		fmt.Println(&quot;The Matrix&quot;)
	case 3:
		age &lt;= 25
		fmt.Println(&quot;John Wick&quot;)
	case 4:
		age &lt;= 35
		fmt.Println(&quot;Constantine&quot;)
	case 5:
		age &gt; 35
		fmt.Println(&quot;Speed&quot;)
	default:
		fmt.Println(&quot;Error: Unknown age&quot;)
	}
}

I get a yellow marker in the IDE for the scanf, flagging an unhandled error. Also, all the cases have the first line flagged red, preventing to compile. The error I get is age &lt;= 14 is evaluated, but not used. The same is true for all statements. I have searched the web and looked at examples, and from what I can see, the code is, as the materials and examples state. Here is a screenshot from Go Land (JetBrains):

Does anybody have an idea? Also, if you answer, please keep in mind, that this is a lesson from a Go course and I have to use scanf. I could change the switch to an else if, which is likely going to fix the issue, but I am kind of curious, about why this is happening and how I can fix it. how to use a `switch` statement in Go

答案1

得分: 1

age &lt;= 14 不是一个有效的 Go 表达式,尽管它可以作为一个表达式的一部分使用。Go 对于所有的值都要求被消耗非常严格。

这不是使用 &lt;= 运算符编写 case 的正确方式。

    switch age {
    case 1:
        age &lt;= 14
        fmt.Println("Toy Story 4")
    ...
    }

这个 case 会在 age 的值为 1 时匹配。你应该使用以下的 switch 样式:

switch {
case age &lt;= 14:
   fmt.Println("Toy Story 4")
case age &lt;= 18:
   ...
...
}
英文:

age &lt;= 14 is not a valid Go expression, though it could be used as part of one. Go is very particular about all values being consumed.

This isn't the correct way to write a case with &lt;= operators.

    switch age {
    case 1:
        age &lt;= 14
        fmt.Println(&quot;Toy Story 4&quot;)
    ...
    }

That case matches if the value of age is 1. You want to use a switch style like this:

switch {
case age &lt;= 14:
   fmt.Println(&quot;Toy Story 4&quot;)
case age &lt;= 18:
   ...
...
}

答案2

得分: 1

为了补充Daniel所写的内容 - 基本上,switch语句(除非是类型switch,但我们不要离题)有两种形式:

switch { // 注意这里没有任何表达式
  case bool_expr_1:
    ...
  case bool_expr_2:
    ...

switch any_expr {
  case value_to_match_1:
    ...
  case value_to_match_2:
    ...

在第一种形式中,每个case分支中的表达式从上到下进行评估,并且首个评估为true的表达式会“胜出” - 也就是说,执行该分支中的代码。
在第二种形式中,表达式被评估为产生一个值,然后将该值与每个case分支中的值进行精确比较。

你试图将这两种形式结合起来,但这是行不通的。
你可能认为case分支中的那些数字是分支应该被检查的顺序,但实际上不是这样的。

英文:

To add to what Daniel wrote — basically the switch statement (unless it's a type switch but let's not digress) has two forms:

switch { // note the absense of any expression here
  case bool_expr_1:
    ...
  case bool_expr_2:
    ...

and

switch any_expr {
  case value_to_match_1:
    ...
  case value_to_match_2:
    ...

In the first form, expressions in each case branch are evaluated top-to-bottom, and the first which evaluates to true, "wins"—that is, the code in that branch is executed.
In the second form, the expression is evaluated to produce a value which is then compared exactly with the value in each case branch.

You have tried to sort-of combine both forms, which won't work.
You might have thought that those numbers in the case branches is the order of how the branches should be checked but no, that's not it.

huangapple
  • 本文由 发表于 2021年12月22日 00:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/70438741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定