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

huangapple go评论82阅读模式
英文:

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

问题

在Go语言中,有没有内置的方法可以获取文件类型?

仅仅通过文件名(在Windows中)猜测文件类型是否足够,还是有一种方法可以基本上判断文件是否是可执行文件?我想找到一种跨平台的方法来实现这个功能,而仅仅读取文件名可能会错过将可执行文件重命名为*.jpg的情况。

英文:

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.

答案1

得分: 5

你可以使用标准库中的mime.TypeByExtensionhttp.DetectContentType来进行MIME类型的猜测,或者使用第三方的libmagic绑定库,例如magicmime

英文:

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.

huangapple
  • 本文由 发表于 2014年2月13日 03:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/21738090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定