A simple project to compare different Go projects/packages that detect MIME types.
Library | Implementation | Stars | Contributors |
---|---|---|---|
http.DetectContentType | Pure Go | ||
gabriel-vasile/mimetype | Pure Go | ||
zRedShift/mimemagic | Pure Go | ||
h2non/filetype | Pure Go | ||
vimeo/go-magic/magic | libmagic | ||
rakyll/magicmime | libmagic |
Dependency:
Some go packages for MIME type detection are libmagic wrappers.
$ brew install libmagic
Build:
$ go build
Use:
$ ./go-mime-lib-compare > out.md
See comparison results.
- gabriel-vasile/mimetype is written in pure Go and has good detection capablities and uses a hierarchical detection structure.
- Go wrappers of libmagic have the most accurate MIME type detection.
However it entails use of cgo. Further, libmagic is not threadsafe
and hence Go wrappers will have to use either
sync.Mutex
orruntime.LockOSThread
. - Although h2non/filetype is written in pure Go, it doesn't detect a variety of types. Further, surprisingly, its detection is not deterministic. IOW, for the same input file, MIME type detected can be different on each run! This is not recommended for any production use.