go - 构建时出错,得到 : "suspect or "

标签 go debugging

我在使用 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/

相关文章:

debugging - 如何在 Vim 中找出某个快捷键触发了哪个命令

c - fork() 返回值错误

memory-management - 为什么我的 Go 程序内存波动这么大?

go - 如何将接口(interface)转换为自定义类型

go - 如何检查一个go channel是否真的在等待数据?

python - 绝对导入和管理包的 "live"和 "development"版本

debugging - boost 测试 : how to debug tested code?

go - 在 Golang 中,我可以将 slice 值分配为返回值吗?

json - 有什么方便的方法可以在没有类型断言的情况下获取 JSON 元素吗?

macos - 当检查器打开时,Chrome 会随机中断,没有断点