To implement custom MIME type handling for specific file extensions in Go, you can follow these steps:
mime
and net/http
packages in your Go file.import (
"mime"
"net/http"
)
mime.AddExtensionType
function. Define the MIME type you want to associate with the file extension.func init() {
mime.AddExtensionType(".custom", "application/x-custom")
}
func serveCustomFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-custom")
http.ServeFile(w, r, "path/to/file.custom") // Replace with the actual file path
}
http.ServeMux
or http.Handle
function.func main() {
mux := http.NewServeMux()
mux.HandleFunc("/custom", serveCustomFile)
http.ListenAndServe(":8080", mux)
}
/custom
URL.Make sure to replace "path/to/file.custom"
with the actual file path you want to serve. Additionally, you can modify the MIME type and the extension according to your requirements.