Home > OS >  How does go infer a parameter value in a function call?
How does go infer a parameter value in a function call?

Time:09-25

I am fairly new to golang but with exp with other OOP languages (java's lambda as well).

How does the call router.GET("/", baseHandler) works when a parameter is not explicitly passed in baseHandler and 2nd argument in router.GET expects ...Handlers

package main
import "github.com/gin-gonic/gin"
func baseHandler(c *gin.Context){
   c.JSON(200, gin.H{
       "message": "hello world",
   })
}
func main() {
   router := gin.Default()
   router.GET("/", baseHandler)
   router.Run()
}

CodePudding user response:

A Handler is of type HandlerFunc, which is

type HandlerFunc func(*Context)

So router.GET is passed a function variable, the baseHandler. When the router calls baseHandler, it passes a *Context to it.

CodePudding user response:

Functions can be used as values. baseHandler is not being passed any parameters because it is not being called.

  •  Tags:  
  • go
  • Related