github.com/x04/go/src@v0.0.0-20200202162449-3d481ceb3525/net/http/example_handle_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http_test
     6  
     7  import (
     8  	"github.com/x04/go/src/fmt"
     9  	"github.com/x04/go/src/log"
    10  	"github.com/x04/go/src/net/http"
    11  	"github.com/x04/go/src/sync"
    12  )
    13  
    14  type countHandler struct {
    15  	mu	sync.Mutex	// guards n
    16  	n	int
    17  }
    18  
    19  func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    20  	h.mu.Lock()
    21  	defer h.mu.Unlock()
    22  	h.n++
    23  	fmt.Fprintf(w, "count is %d\n", h.n)
    24  }
    25  
    26  func ExampleHandle() {
    27  	http.Handle("/count", new(countHandler))
    28  	log.Fatal(http.ListenAndServe(":8080", nil))
    29  }