Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does Context struct work in golang gin framework? [closed]

I want to understand what is Context in go gin , i see a lot of functions written which accept context as a parameter but dont see it passed anywhere or instantiated anywhere ? , can someone explain how it works

like image 279
thatguy Avatar asked Oct 29 '25 17:10

thatguy


1 Answers

The gin Context is a structure that contains both the http.Request and the http.Response that a normal http.Handler would use, plus some useful methods and shortcuts to manipulate those.

The gin engine is responsible for the creation (and reuse) of those contexts, in the same manner as the http.Server is responsible for the creation of the http.Request objects a standard http.Handler would use.

The context is passed by the engine to its handlers, and it's your job to write those handlers and attach them to a router. A gin handler is any function that takes a gin.Context as its sole argument and doesn't return anything.

like image 167
Olivier Roux Avatar answered Oct 31 '25 09:10

Olivier Roux