github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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 "github.com/hxx258456/ccgo/gmhttp" 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 }