Last active
November 24, 2015 23:17
-
-
Save enahs/34110bb86f4c0503a36d to your computer and use it in GitHub Desktop.
Mimetype - a simple, command line utility that uses go's http.DetectContentType to get the mimetype of a file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"os" | |
) | |
const ( | |
usage = `usage: mimetype [filename]` | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
println(usage) | |
os.Exit(1) | |
} | |
f, err := os.Open(os.Args[1]) | |
if err != nil { | |
println("could not open file:", err.Error()) | |
os.Exit(1) | |
} | |
defer f.Close() | |
b := make([]byte, 512) | |
_, err = f.Read(b) | |
if err != nil { | |
println("could not read file:", err.Error()) | |
os.Exit(1) | |
} | |
println(http.DetectContentType(b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment