Bart Silverstrim
Bart Silverstrim

Reputation: 3625

Is there a way to tell a file type (executable, text, etc.) in Go?

It there a built-in way to get a file type in Go?

Is it sufficient to guess from the filename alone (in Windows) or is there a way to tell only basically if the file is, say, an executable file? I'd like to find a cross-platform method of doing this, and reading a filename and working from that may miss executable files that are renamed to *.jpg.

Upvotes: 5

Views: 366

Answers (1)

nemo
nemo

Reputation: 57649

You can either use MIME type guessing using mime.TypeByExtension or http.DetectContentType from the standard library or use third-party libmagic bindings, e.g. magicmime.

Upvotes: 5

Related Questions