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
+15k Golang : How do I get the local IP (non-loopback) address ?
+6.5k Unix/Linux : How to get own IP address ?
+8k Golang : Sort words with first uppercase letter
+36.5k Golang : Validate IP address
+5.3k Golang : Pad file extension automagically
+20.7k Android Studio : AlertDialog and EditText to get user string input example
+13.8k Golang : Convert spaces to tabs and back to spaces example
+19.5k Golang : How to Set or Add Header http.ResponseWriter?
+16.5k Golang : File path independent of Operating System
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+8.8k Golang : HTTP Routing with Goji example
+24k Golang : Call function from another package