• Not the issues:
  • Want to know if there is a quick way to get execution time or specific function
  • Request f

As of PHP framework like Codeigniter, Is that possible in Gin

How to reproduce

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    g := gin.Default()
    g.GET("/hello/:name", func(c *gin.Context) {
        c.String(200, "Hello %s", c.Param("name"))
    })
    g.Run(":9000")
}

Expectations

Environment

  • go version:
  • gin version (or commit ref):
  • operating system:

Comment From: sxk10812139

Perhaps you meant this?

startTime := time.Now()
...
endTime := time.Now()

Comment From: ivanruslimcdohl

package main

import (
    "log"
    "time"
)

func main() {
    tStart := time.Now()
    time.Sleep(123 * time.Millisecond)
    execTime := time.Since(tStart)

    log.Printf("executed in %s\n, ", execTime)
}

If you want it in gin Middleware

package main

import (
    "log"
    "time"

    "github.com/gin-gonic/gin"
)

func main() {
    g := gin.Default()
    g.Use(roundTrip())
    g.GET("/hello/:name", func(c *gin.Context) {
        c.String(200, "Hello %s", c.Param("name"))
    })
    g.Run(":9000")
}

func roundTrip() gin.HandlerFunc {
    return func(c *gin.Context) {
        tsReq := time.Now()

        c.Next()

        execTime := time.Since(tsReq)
        log.Printf("%s executed in %s\n, ", c.FullPath(), execTime)
    }
}

Anyway, the default logger in Gin already print out the execution time.

Comment From: Gadrawingz

I am starting to understand!