英文:
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":
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论