我在使用 go 时遇到了构建问题。我想知道这是编译器中的错误还是代码的问题。
// removed the error handling for sake of clarity
file, _ := c.FormFile("file")
openedFile, _ := file.Open()
buffer := make([]byte, 512)
n, _ := openedFile.Read(buffer)
contentType := http.DetectContentType(buffer[:n])
// doesn't work
if contentType != "image/jpeg" || contentType != "image/png" {
return
}
// works
if contentType != "image/jpeg" {
return
}
else if contentType != "image/png" {
return
}
错误
suspect or: contentType != "image/jpeg" || contentType != "image/png"
仅供引用 "c.FormFile("file") "是形式 Gin gonic。但这并不重要。
最佳答案
您看到的是编译器警告,但应用程序将运行。
您的情况总是true
:
contentType != "image/jpeg" || contentType != "image/png"
你比较一个string
可变为 2 个不同的 string
值(使用不相等),所以其中之一肯定是 true
, 和 true || false
总是 true
.很可能您需要逻辑 AND:我假设您想测试内容类型是否既不是 JPEG 也不是 PNG:
if contentType != "image/jpeg" && contentType != "image/png" {
return
}
关于go - 构建时出错,得到 : "suspect or ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62470008/