github.com/ooni/oohttp@v0.7.2/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  	"sync"
    11  
    12  	http "github.com/ooni/oohttp"
    13  )
    14  
    15  type countHandler struct {
    16  	mu sync.Mutex // guards n
    17  	n  int
    18  }
    19  
    20  func (h *countHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    21  	h.mu.Lock()
    22  	defer h.mu.Unlock()
    23  	h.n++
    24  	fmt.Fprintf(w, "count is %d\n", h.n)
    25  }
    26  
    27  func ExampleHandle() {
    28  	http.Handle("/count", new(countHandler))
    29  	log.Fatal(http.ListenAndServe(":8080", nil))
    30  }