gin-gonic / examples

A repository to host examples and tutorials for Gin.

Home Page:https://gin-gonic.com/docs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

context.Next()

achempak-polymer opened this issue · comments

The docs say that examples using next should be present in this repo (https://pkg.go.dev/github.com/gin-gonic/gin#Context.Next). I'm a bit confused on the lifecycle of middleware. For example, say we have the following middleware:

func timerMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		t1 := time.Now()
		c.Next()
                t2 := time.Now().Sub(t1)
                c.Header("time", t2.Seconds())
	}
}

My goal here is to include the time it took to process the request as a response header. Is the above correct way of doing this?