Golang net/http.Server.Serve() and SetKeepAlivesEnabled() functions example
package net/http
Golang net/http.Server.Serve() and SetKeepAlivesEnabled() functions usage example
package main
import (
"fmt"
"net"
"net/http"
)
func home(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", home)
server := &http.Server{Handler: mux}
server.Addr = ":8080"
netListener, err := net.Listen("tcp", ":9000") // change port to 9000
if err != nil {
fmt.Println(err)
}
// pointing your browser to :8080 no longer work
// point to port :9000 instead because the server is listening there
err = server.Serve(netListener)
server.SetKeepAlivesEnabled(false) // change from default true
if err != nil {
fmt.Println(err)
}
}
References :
See also : Golang net.Listen() function example
Advertisement
Something interesting
Tutorials
+7.6k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+7.4k Golang : Check to see if *File is a file or directory
+13.8k Generate salted password with OpenSSL example
+7.5k Golang : Process json data with Jason package
+4.7k Adding Skype actions such as call and chat into web page examples
+12.1k Golang : convert(cast) string to integer value
+15.2k Golang : Get timezone offset from date or timestamp
+31.1k Golang : Calculate percentage change of two values
+25.4k Golang : Convert long hexadecimal with strconv.ParseUint example
+5.5k Golang : Display advertisement images or strings on random order
+9.8k Golang : Qt get screen resolution and display on center example