Created
April 16, 2025 12:25
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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