Get mime type

This is an example Go program demonstrating getting the mime type of a file extension.

package main

import (
        "fmt"
        "mime"
        "os"
)

func main() {
        for i, arg := range os.Args {
                if i == 0 {
                        continue
                }
                mimeType := mime.TypeByExtension("." + arg)
                if len(mimeType) > 0 {
                        fmt.Printf("Extension '.%s' has mime type %s.\n",
                                arg, mimeType)
                } else {
                        fmt.Printf("Extension '.%s' is unknown.\n",
                                arg)
                }
        }
}

(download)

The output of

./gmt  html frog fish bone t sh

looks like this:

./gmt  html frog fish bone t sh
Extension '.html' has mime type text/html; charset=utf-8.
Extension '.frog' is unknown.
Extension '.fish' is unknown.
Extension '.bone' is unknown.
Extension '.t' has mime type application/x-troff.
Extension '.sh' has mime type text/x-sh; charset=utf-8.


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer