0PricingLogin
Go Academy · Lesson

Response Helpers and Status Codes

c.JSON, c.AbortWithError, and error responses

c.JSON

c.JSON(statusCode, obj) serialises obj to JSON and sets Content-Type to application/json. It is the most common response method.

c.JSON(http.StatusOK, gin.H{
    "id":   user.ID,
    "name": user.Name,
})

gin.H

gin.H is a shorthand for map[string]any. Use it for ad-hoc JSON objects without defining a struct.

c.JSON(200, gin.H{"message": "created", "id": id})

All lessons in this course

  1. Gin Router and Route Groups
  2. Request Binding and Validation
  3. Response Helpers and Status Codes
  4. Gin Middleware: Auth and Logging
← Back to Go Academy