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
+22.4k Golang : How to read JPG(JPEG), GIF and PNG files ?
+17.7k How to enable MariaDB/MySQL logs ?
+16.4k Golang : Send email and SMTP configuration example
+28.6k Golang : Read, Write(Create) and Delete Cookie example
+16.5k Golang : Get IP addresses of a domain name
+30.9k error: trying to remove "yum", which is protected
+14.6k Golang : Send email with attachment(RFC2822) using Gmail API example
+5.4k Python : Delay with time.sleep() function example
+18.1k Golang : Convert IPv4 address to decimal number(base 10) or integer
+7.8k Golang : Reverse a string with unicode
+8.6k Golang : Progress bar with ∎ character
+6k Golang : How to verify input is rune?