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
+13.4k Golang : Verify token from Google Authenticator App
+12.7k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+11.3k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+6.1k Golang : Missing Subversion command
+9.6k Golang : Read file with ioutil
+7.9k Javascript : Put image into Chrome browser's console
+11k Golang : Create Temporary File
+11.1k Golang : Roll the dice example
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+8.6k Android Studio : Import third-party library or package into Gradle Scripts
+8.2k Golang : Reverse text lines or flip line order example
+9.4k Facebook : Getting the friends list with PHP return JSON format