开关或操作符编译错误

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

Switch or operator compiler error

问题

我有以下代码,编译器报错。

switch req.Method {
    case "POST" || "PUT" || "DELETE":
        if req.Header.Get("Content-Type") != "application/json" {
            return handleErr(req)
        }
}

编译器错误信息:

..\..\controllers\routes\header.go:59: invalid operation: "POST" || "PUT" (operator || not defined on string)

我是不是错误地使用了或运算符?

英文:

I have following code that compiler complain.

switch req.Method {
        case "POST" || "PUT" || "DELETE":
            if req.Header.Get("Content-Type") != "application/json" {
                return handleErr(req)
            }
    }

Compiler error message

..\..\controllers\routes\header.go:59: invalid operation: "POST" || "PUT" (operator || not defined on string)

Do I use OR operator in wrong way?

答案1

得分: 3

只需使用逗号 , 替代 ||

case "POST", "PUT", "DELETE":

参见switch语句参考

英文:

Just use comma , instead of ||.

case "POST", "PUT", "DELETE":

See switch statement reference.

huangapple
  • 本文由 发表于 2015年3月2日 18:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/28806972.html
匿名

发表评论

匿名网友

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

确定