func myHandler(ctx *context.Context) { // set a context value ctx.Data["myContextValue"] = "foobar" // retrieve the context value myValue := ctx.Query("myContextValue") fmt.Println(myValue) // prints "foobar" }
func someMiddleware(ctx *context.Context) { // set a context value ctx.Data["myContextValue"] = "something" // call the next middleware/handler ctx.Next() } func myHandler(ctx *context.Context) { // retrieve the context value myValue := ctx.Query("myContextValue") fmt.Println(myValue) // prints "something" } func main() { r := gin.Default() r.Use(someMiddleware) // this middleware sets the context value r.GET("/", myHandler) // this handler retrieves the context value r.Run() }In both examples, the context value is set using `ctx.Data["myContextValue"]` and retrieved using `ctx.Query("myContextValue")`. The `context.Context` object is provided by the package library and can be used in any middleware or handler.