error when building , getting : "suspect or "

Viewed 14122

I'm encountering a build issue with go. I'm wondering if it's a bug in the compiler or a problem with the code.

// 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
}

error suspect or: contentType != "image/jpeg" || contentType != "image/png"

fyi " c.FormFile("file") " is form Gin gonic. but it shouldnt really matter.

1 Answers

What you see is a compiler warning, but the app will run.

Your condition is always true:

contentType != "image/jpeg"  || contentType != "image/png" 

You compare a string variable to 2 different string values (using not equal), so one of them will surely be true, and true || false is always true.

Most likely you need logical AND: I assume you want to test if the content type is neither JPEG nor PNG:

if contentType != "image/jpeg" && contentType != "image/png" {
    return 
}
Related