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
+23.1k Golang : Randomly pick an item from a slice/array example
+13.6k Golang : Qt progress dialog example
+17.6k Convert JSON to CSV in Golang
+17.9k Golang : How to make a file read only and set it to writable again?
+7.4k Golang : How to detect if a sentence ends with a punctuation?
+10.8k Golang : Command line file upload program to server example
+4.8k Facebook : How to place save to Facebook button on your website
+29.4k Golang : JQuery AJAX post data to server and send data back to client example
+31.6k Golang : Get local IP and MAC address
+15.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+15.3k Golang : Get all local users and print out their home directory, description and group id
+8k Golang : Handle Palindrome string with case sensitivity and unicode