Created
June 26, 2019 06:25
-
-
Save isaacd9/2aa06aceaa558b9d880d7147af395c2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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