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