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
+10.2k Golang : Get login name from environment and prompt for password
+20.9k Golang : Underscore or snake_case to camel case example
+9.4k Golang : How to get garbage collection data?
+23k Golang : Test file read write permission example
+8.4k Golang: Prevent over writing file with md5 hash
+21.7k Golang : Encrypt and decrypt data with TripleDES
+18.9k Golang : Implement getters and setters
+7.9k Golang : Example of how to detect which type of script a word belongs to
+8.5k PHP : How to parse ElasticSearch JSON ?
+20.3k Golang : Compare floating-point numbers
+20.3k Golang : How to get struct tag and use field name to retrieve data?
+9.1k Golang : Build and compile multiple source files