我遇到了一个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格式。但这真的无关紧要。
发布于 2020-06-19 20:13:48
您看到的是一个编译器警告,但应用程序将会运行。
您的条件始终为true
contentType != "image/jpeg" || contentType != "image/png"
您将一个string
变量与两个不同的string
值进行比较(使用不相等),因此其中一个肯定是true
,而true || false
始终是true
。
您最可能需要逻辑AND:我假设您想测试内容类型是否既不是JPEG也不是PNG:
if contentType != "image/jpeg" && contentType != "image/png" {
return
}
https://stackoverflow.com/questions/62470008
复制相似问题