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
+9.4k Golang : Terminate-stay-resident or daemonize your program?
+9.6k Golang : Validate IPv6 example
+17.1k Golang : XML to JSON example
+12.3k Golang : List running EC2 instances and descriptions
+11.6k Golang : Concurrency and goroutine example
+6k Golang : Function as an argument type example
+6.9k Nginx : Password protect a directory/folder
+5.3k Javascript : Change page title to get viewer attention
+6.1k Golang : Dealing with backquote
+15k Golang : package is not in GOROOT during compilation
+24k Golang : Find biggest/largest number in array
+29.9k Golang : Get and Set User-Agent examples