gothinkster / golang-gin-realworld-example-app

Exemplary real world application built with Golang + Gin
https://realworld.io
MIT License
2.5k stars 495 forks source link

why using gin.context in serizlizers #17

Closed sorushsaghari closed 3 years ago

sorushsaghari commented 5 years ago

hello guys . i dont understand why all serializers have a *gin.Context in them like these.

type ProfileSerializer struct {
    C *gin.Context
    UserModel
}
type CommentSerializer struct {
    C *gin.Context
    CommentModel
}
VictorAvelar commented 5 years ago

Context in go is a communication and cancellation mechanism that can be passed across domain boundaries.

I am not a gin expert, but according to their docs:

Context is the most important part of gin. It allows us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.

You might also find interesting to read the code itself here as it will help you understand how they use context to glue the mentioned features.

context.go

wangzitian0 commented 4 years ago

Gin wrapped many information about the http protocol inside the context, more content can be find in the official doc: https://godoc.org/github.com/gin-gonic/gin#Context For real world, we need to read the session/cookie or user information, we can read it from the context.