gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmhttp/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 gmhttp_test
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"sync"
    11  
    12  	http "gitee.com/ks-custle/core-gm/gmhttp"
    13  )
    14  
    15  type countHandler struct {
    16  	mu sync.Mutex // guards n
    17  	n  int
    18  }
    19  
    20  //goland:noinspection GoUnusedParameter
    21  func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    22  	h.mu.Lock()
    23  	defer h.mu.Unlock()
    24  	h.n++
    25  	_, _ = fmt.Fprintf(w, "count is %d\n", h.n)
    26  }
    27  
    28  func ExampleHandle() {
    29  	http.Handle("/count", new(countHandler))
    30  	log.Fatal(http.ListenAndServe(":8080", nil))
    31  }