Golang mime.TypeByExtension() function example

package mime

Golang mime.TypeByExtension() function usage example

 package main

 import (
 "fmt"
 "mime"
 "os"
 "path/filepath"
 )

 func main() {

 if len(os.Args) != 2 {
 fmt.Printf("Usage : %s filename \n", os.Args[0])
 os.Exit(1)
 }

 // get file extension

 ext := filepath.Ext(os.Args[1])

 mType := mime.TypeByExtension(ext)

 fmt.Println("Media type : ", mType)

 }

SEE ALSO : https://www.socketloop.com/tutorials/golang-how-to-verify-uploaded-file-is-image-or-allowed-file-types

References :

https://www.socketloop.com/tutorials/golang-find-files-by-extension

http://golang.org/pkg/mime/#TypeByExtension

Advertisement