Skip to content

Instantly share code, notes, and snippets.

@tanapoln
Created September 15, 2020 15:58
Show Gist options
  • Save tanapoln/df28c121eb7e041f4a3697e57c3ca2d4 to your computer and use it in GitHub Desktop.
Save tanapoln/df28c121eb7e041f4a3697e57c3ca2d4 to your computer and use it in GitHub Desktop.
Simple page view counter atomic
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