How to check filetype in golang (equlvalent of filetype cmd in linux )

3,256 views
Skip to first unread message

Rahul R

unread,
Aug 10, 2013, 4:40:56 PM8/10/13
to golang-nuts
Hey guys ,

What is the easiest way to check the filetypes using golang. I shud be able to know, whether its a regualr ascii file or berkely db file or executable.  similar to filetype command in golang.  How do I check the same ?


Thanks,
./Rahul 

Andy Balholm

unread,
Aug 10, 2013, 5:13:38 PM8/10/13
to golan...@googlegroups.com
You could use the os/exec package to run your system's "file" command.

Or you could find the C source of the "file" command and port it to Go. I think it uses a library called libmagic. Maybe you could link to that with CGO.

Or you could look on godoc.org for Go bindings for libmagic. I think there are at least two. I also see a less exhaustive pure-Go library that uses the http package's content sniffing.

Ibrahim M. Ghazal

unread,
Aug 11, 2013, 7:41:37 AM8/11/13
to Rahul R, golang-nuts
Reply all
Reply to author
Forward
0 new messages