Golang net/http.Error() function example
package net/http
Golang net/http.Error() function usage example
package main
import (
"net/http"
)
func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
html := "Hello"
html = html + " World"
w.Write([]byte(html))
}
func SayError(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Say Error!", 500)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", SayHelloWorld)
mux.HandleFunc("/error", SayError)
http.ListenAndServe(":8080", mux)
}
Sample output :
From browser :
Point to URL [your server]:8080/error
Say Error!
From terminal :
curl -I [your server]:8080/error
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain; charset=utf-8
Date: Mon, 25 May 2015 06:29:48 GMT
Content-Length: 11
See Also : https://www.socketloop.com/tutorials/golang-how-to-return-http-status-code
Reference :
Advertisement
Something interesting
Tutorials
+4.9k Nginx and PageSpeed build from source CentOS example
+9.3k Golang : How to get username from email address
+25.2k Golang : Storing cookies in http.CookieJar example
+9.7k PHP : Get coordinates latitude/longitude from string
+17.7k Golang : Read data from config file and assign to variables
+21.2k Golang : How to force compile or remove object files first before rebuild?
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+8.5k Linux/Unix : fatal: the Postfix mail system is already running
+7.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+19.9k Golang : How to get time from unix nano example
+15.3k Golang : Delete certain files in a directory
+15.6k Golang : rune literal not terminated error