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
+41k Golang : How to check if a string contains another sub-string?
+6.4k Golang : Handling image beyond OpenCV video capture boundary
+5k Golang : micron to centimeter example
+5.6k Swift : Get substring with rangeOfString() function example
+12.3k Golang : How to display image file or expose CSS, JS files from localhost?
+11.9k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+30k Golang : Get time.Duration in year, month, week or day
+7.3k Golang : Calculate how many weeks left to go in a given year
+10.5k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+10.9k Golang : Sieve of Eratosthenes algorithm
+4.9k Nginx and PageSpeed build from source CentOS example
+6k Linux/MacOSX : Search for files by filename and extension with find command