Skip to content

Instantly share code, notes, and snippets.

@smiley-yoyo
Created April 16, 2025 12:25
Show Gist options
  • Select an option

  • Save smiley-yoyo/ae031829cffe1fc6811c21a1c5f257d4 to your computer and use it in GitHub Desktop.

Select an option

Save smiley-yoyo/ae031829cffe1fc6811c21a1c5f257d4 to your computer and use it in GitHub Desktop.
Considering the definition of a binary file as "a computer file that is not a text file", they can differentiated by searching for the text/plain MIME in their MIME hierarchy.
package main
import (
"fmt"
"github.com/gabriel-vasile/mimetype"
)
func main() {
testBytes := []byte("This random text has a MIME type of text/plain; charset=utf-8.")
detectedMIME := mimetype.Detect(testBytes)
isBinary := true
for mtype := detectedMIME; mtype != nil; mtype = mtype.Parent() {
if mtype.Is("text/plain") {
isBinary = false
}
}
fmt.Println(isBinary, detectedMIME)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment