package main
import (
"fmt"
"net/http"
"os"
)
func GetFileContentType(file *os.File) (string, error) {
buffer := make([]byte, 512)
_, err := file.Read(buffer)
if err != nil {
return "", err
}
contentType := http.DetectContentType(buffer)
return contentType, nil
}
func main() {
file, err := os.Open("data.txt")
if err != nil {
fmt.Println(err)
} else {
stat, _ := file.Stat()
fmt.Println("File Name: ", stat.Name())
fmt.Println("File Size(bytes): ", stat.Size())
fileType, _ := GetFileContentType(file)
fmt.Println("File Type: ", fileType)
}
}
File Name: data.txt
File Size(bytes): 5
File Type: application/octet-stream