登录
首页 >  Golang >  Go问答

疑虑出现在构建时

来源:stackoverflow

时间:2024-02-25 08:36:23 192浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《疑虑出现在构建时》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我遇到了 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 或:contenttype != "image/jpeg" ||内容类型!=“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 
}

以上就是《疑虑出现在构建时》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>