Skip to content

Instantly share code, notes, and snippets.

@isaacd9
Created June 26, 2019 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacd9/2aa06aceaa558b9d880d7147af395c2e to your computer and use it in GitHub Desktop.
Save isaacd9/2aa06aceaa558b9d880d7147af395c2e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
"sync"
)
type counter struct {
sync.Mutex
count uint64
}
func (c *counter) inc() uint64 {
c.Lock()
defer c.Unlock()
cur := c.count
c.count++
return cur
}
func main() {
var c counter
http.HandleFunc("/inc", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%d", c.inc())
})
log.Printf("starting server on 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment