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
+8.2k Golang : Get all countries phone codes
+13.8k Android Studio : Password input and reveal password example
+10.9k Golang : Natural string sorting example
+13k Python : Convert IPv6 address to decimal and back to IPv6
+33.8k Golang : How to check if slice or array is empty?
+31k Golang : Download file example
+6k Golang : Shuffle array of list
+28.2k Golang : Move file to another directory
+5.6k PHP : Fix Call to undefined function curl_init() error
+13.6k Facebook PHP getUser() returns 0
+8.1k Findstr command the Grep equivalent for Windows
+9.5k Golang : Play .WAV file from command line