github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/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  	"fmt"
     9  	"log"
    10  	"net/http"
    11  	"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  }