Golang net/http.NotFound() function example
package net/http
Golang net/http.NotFound() function usage example
package main
import (
"net/http"
)
func returnCode200(w http.ResponseWriter, r *http.Request) {
// see http://golang.org/pkg/net/http/#pkg-constants
w.WriteHeader(http.StatusOK)
w.Write([]byte("☄ HTTP status code returned!"))
}
func returnCode500(w http.ResponseWriter, r *http.Request) {
// see http://golang.org/pkg/net/http/#pkg-constants
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("☄ HTTP status code returned!"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", returnCode200)
mux.HandleFunc("/fivehundred", returnCode500)
mux.HandleFunc("/notfound", http.NotFound) <---- here !
http.ListenAndServe(":8080", mux)
}
Sample output :
URL : http://
:8080/notfound 404 page not found
See also : https://www.socketloop.com/tutorials/golang-how-to-return-http-status-code
Reference :
Advertisement
Something interesting
Tutorials
+5.7k List of Golang XML tutorials
+19.9k Golang : Measure http.Get() execution time
+7.6k Javascript : Push notifications to browser with Push.js
+5.2k Responsive Google Adsense
+17k Golang : Covert map/slice/array to JSON or XML format
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+5.4k Golang : Intercept, inject and replay HTTP traffics from web server
+32.2k Golang : Convert []string to []byte examples
+21.1k Golang : Convert(cast) string to rune and back to string example
+14.2k Golang : Fix image: unknown format error
+10.1k Golang : How to tokenize source code with text/scanner package?
+6.3k Golang : How to search a list of records or data structures