首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >生成时出错,获取:"suspect or“

生成时出错,获取:"suspect or“
EN

Stack Overflow用户
提问于 2020-06-19 20:11:05
回答 1查看 7.3K关注 0票数 12

我遇到了一个go的构建问题。我想知道是编译器中的错误还是代码的问题。

代码语言:javascript
运行
复制
// 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格式。但这真的无关紧要。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-19 20:13:48

您看到的是一个编译器警告,但应用程序将会运行。

您的条件始终为true

代码语言:javascript
运行
复制
contentType != "image/jpeg"  || contentType != "image/png" 

您将一个string变量与两个不同的string值进行比较(使用不相等),因此其中一个肯定是true,而true || false始终是true

您最可能需要逻辑AND:我假设您想测试内容类型是否既不是JPEG也不是PNG:

代码语言:javascript
运行
复制
if contentType != "image/jpeg" && contentType != "image/png" {
    return 
}
票数 41
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62470008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档