GIN-Gonic Flashcards
What is the Gin framework
“A high-performance HTTP web framework for Go designed for building fast and efficient web apps/APIs”
How do you install Gin
“Run the command: go get -u github.com/gin-gonic/gin”
What does the gin.Default() function do
“Creates a Gin router with default middleware (logging and recovery)”
How to define a GET route for the root path in Gin
“r.GET(“/”, handlerFunction)”
What method sends a plain text response in Gin
“c.String(statusCode, content)”
How to start a Gin server on port 8080
“r.Run(“:8080”)”
How to extract a path parameter named ““name”” in Gin
“Use c.Param(““name””)”
How to access a query parameter ““q”” in Gin
“Use c.Query(““q””)”
What is middleware in Gin
“Functions that process requests before reaching the final handler (e.g., logging/auth)”
How to create custom middleware for authentication
“Define a function that checks tokens and uses c.AbortWithStatusJSON() or c.Next()”
How to apply middleware to a specific route
“Add the middleware as an argument in the route definition: r.GET(“/secure”, AuthMiddleware, handler)”
What are route groups used for in Gin
“To organize routes with shared prefixes or middleware”
How to serve static files from a “./static” directory
“Use r.Static(“/static”, “./static”)”
How to bind JSON data to a struct in Gin
“Use c.ShouldBindJSON(&structVariable) inside the handler”
What is the purpose of binding tags like ““binding:”“required”””
“To enforce validation rules for incoming data”
How to handle file uploads in Gin
“Use c.FormFile(““file””) and c.SaveUploadedFile()”
How to send a JSON response in Gin
“Use c.JSON(statusCode, data)”
How to render HTML templates in Gin
“Load templates with r.LoadHTMLGlob() and use c.HTML() in handlers”
What does the Recovery middleware do
“Handles panics gracefully and prevents server crashes”
How to create a custom validation rule like ““is_cool”” in Gin
“Register it via validator.Validate.RegisterValidation()”
How to test Gin handlers
“Use Go’s httptest package to simulate requests and record responses”
What command builds a production binary for a Gin app
“go build -o app”
How to run Gin in production mode
“Set environment variable: GIN_MODE=release”
Name two best practices for Gin deployment
“Use structured logging and implement graceful shutdown”