Created
September 15, 2020 15:59
-
-
Save tanapoln/84865a9ad67c86c57b7ac90f08a717f4 to your computer and use it in GitHub Desktop.
Simple page view counter - mutex
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" | |
"sync" | |
"github.com/gin-gonic/gin" | |
) | |
func main() { | |
counter := 0 | |
var m sync.Mutex | |
r := gin.Default() | |
r.GET("/", func(c *gin.Context) { | |
m.Lock() | |
defer m.Unlock() | |
counter++ | |
c.String(200, "Hello world") | |
}) | |
r.GET("/stats", func(c *gin.Context) { | |
c.String(200, fmt.Sprintf("Number of page view: %d", counter)) | |
}) | |
r.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment