Skip to content

Instantly share code, notes, and snippets.

@enahs
Last active November 24, 2015 23:17
Show Gist options
  • Save enahs/34110bb86f4c0503a36d to your computer and use it in GitHub Desktop.
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.
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