【发布时间】:2021-11-14 00:34:47
【问题描述】:
我在使用 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"
fyi " c.FormFile("file") " 是形式 Gin gonic。但这并不重要。
【问题讨论】:
-
除非是嫌疑人或:您的 if 语句将始终为真,因为
contentType不能同时等于image/jpeg和image/png。 -
使用 Joe Average 类型代码检测到实际编译器错误的机会为零。
-
当谷歌搜索时只有 2 个帖子出现,这两个问题都是引用来自编译器的错误或
-
请注意,
if ... { ... return } else始终是多余的。删除else。