Back to list

Gin Web Framework API

Lv.5755@mukitaro12 playsDec 28, 2025

Gin Web Framework REST API. Fastest Go web framework.

preview.go
Go
1package main
2
3import (
4 "net/http"
5
6 "github.com/gin-gonic/gin"
7)
8
9type Book struct {
10 ID string `json:"id"`
11 Title string `json:"title"`
12 Author string `json:"author"`
13}
14
15var books = []Book{
16 {ID: "1", Title: "Go Programming", Author: "John"},
17 {ID: "2", Title: "Web Development", Author: "Jane"},
18}
19
20func getBooks(c *gin.Context) {
21 c.IndentedJSON(http.StatusOK, books)
22}
23
24func postBook(c *gin.Context) {
25 var newBook Book
26 if err := c.BindJSON(&newBook); err != nil {
27 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
28 return
29 }
30 books = append(books, newBook)
31 c.IndentedJSON(http.StatusCreated, newBook)
32}
33
34func main() {
35 router := gin.Default()
36 router.GET("/books", getBooks)
37 router.POST("/books", postBook)
38 router.Run(":8080")
39}

Custom problems are not included in rankings