Fragment Description:

It is more than worthwhile to know the Go http.Server type.
It is a rich one with many methods.
See:// A Server defines parameters for running an HTTP server.
The zero value for Server is a valid configuration.
Here the use of readTimeout and writeTimeout.


Last update, on 2015, Tue 1 Dec, 15:31:36

/* ... <== see fragment description ... */

package main

import (

const (
    readTimeout  = time.Duration(1 * time.Second)
    writeTimeout = readTimeout

var (
    port int

func homeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, from home!")
func slowHandler(w http.ResponseWriter, r *http.Request) {
    log.Println("We have started running slowish...")


    fmt.Fprint(w, "You should not see this, I'm too slow!")
func init() {
    flag.IntVar(&port, "port", 8080, "HTTP Server Port")
func main() {
    httpAddr := fmt.Sprintf(":%v", port)
    log.Printf("Listening on port:%v ...\n", httpAddr)

    http.HandleFunc("/slowish", slowHandler)
    http.HandleFunc("/", homeHandler)

    srv := http.Server{
        Addr:         httpAddr,
        ReadTimeout:  readTimeout,
        WriteTimeout: writeTimeout,

/* the net/http Server type:
type Server struct {
        // TCP address to listen on, ":http" if empty
       Addr           string

// handler to invoke, http.DefaultServeMux if nil
       Handler        Handler

// maximum duration before timing out read of the request
       ReadTimeout    time.Duration

// maximum duration before timing out write of the response
       WriteTimeout   time.Duration

// maximum size of request headers, DefaultMaxHeaderBytes if 0
       MaxHeaderBytes int

// optional TLS config, used by ListenAndServeTLS
       TLSConfig      *tls.Config
        // TLSNextProto optionally specifies a function to take over
        // ownership of the provided TLS connection when an NPN
        // protocol upgrade has occurred.  The map key is the protocol
        // name negotiated. The Handler argument should be used to
        // handle HTTP requests and will initialize the Request's TLS
        // and RemoteAddr if not already set.  The connection is
        // automatically closed when the function returns.
        TLSNextProto map[string]func(*Server, *tls.Conn, Handler)
        // ConnState specifies an optional callback function that is
        // called when a client connection changes state. See the
        // ConnState type and associated constants for details.
        ConnState func(net.Conn, ConnState)
        // ErrorLog specifies an optional logger for errors accepting
        // connections and unexpected behavior from handlers.
        // If nil, logging goes to os.Stderr via the log package's
        // standard logger.
        ErrorLog *log.Logger
        // contains filtered or unexported fields
And these methods:
func (srv *Server) ListenAndServe() error
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error
func (srv *Server) Serve(l net.Listener) error
func (*Server) SetKeepAlivesEnabled
See: See://