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, _ := os.Open("a.txt")
fileType, _ := GetFileContentType(file)
fmt.Println("file type:", fileType)
}
Output
file type: application/octet-stream