Golang net/http.ServeContent() function example
package net/http
Golang net/http.ServeContent() function usage example.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
name := *dir + r.URL.Path
file, err := os.Open(name)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Unable to open and read file : %v", err))
return
}
defer f.Close()
http.ServeContent(w, r, name, time.Now(), file)
})
References :
https://www.socketloop.com/tutorials/golang-how-to-return-http-status-code
Advertisement
Something interesting
Tutorials
+6.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+11k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+3.7k Chrome : How to block socketloop.com links in Google SERP?
+16.1k Golang : Qt image viewer example
+14.6k Golang : How to reverse elements order in map ?
+7.1k Golang : HTTP Server Example
+6.8k Golang : Scan files for certain pattern and rename part of the files
+19.1k Golang : How to force compile or remove object files first before rebuild?
+20.8k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+13.9k Golang : Find location by IP address and display with Google Map
+3.8k Facebook : How to place save to Facebook button on your website
+4.9k Golang : List all packages and search for certain package