英文:
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.TypeByExtension或http.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论