Detecting MIME Types in Go
1 points by runxiyu
1 points by runxiyu
Not to be “that guy”, but did a human write this? The language is very weirdly flowery
Whether you’re safeguarding against the wrong file types in an upload process or curating content based on its nature, understanding and utilizing MIME type detection is an invaluable skill.
In this snippet, DetectContentType() takes the stage, examining the initial bytes of the file and returning a MIME type, such as image/png. Should it find itself at a loss, unable to pin down the file’s type, it defaults to application/octet-stream, a way of saying, “This is a file, but beyond that, you’re on your own.”
You may expect to find this functionality in the file package, but you’d be wrong! Read on…
That package does not exist.
Why net/http?
Because it implements the MIME Sniffing standard, which was written to handle http responses without a Content-Type header.