gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:30:58 53 4
gpt4 key购买 nike

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

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com