Created
March 9, 2022 12:01
-
-
Save xvbnm48/0447e95067b001bf8faf4d385647f4f3 to your computer and use it in GitHub Desktop.
handler for golang web api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rootHandler(c *gin.Context) { | |
c.JSON(200, gin.H{ | |
"Name": "sakura endo", | |
"Age": 20, | |
"Address": "Tokyo", | |
}) | |
} | |
func helloHandler(c *gin.Context) { | |
c.JSON(200, gin.H{ | |
"Name": "sakura endo", | |
"Age": 20, | |
"Address": "Tokyo", | |
}) | |
} | |
func booksHandler(c *gin.Context) { | |
id := c.Param("id") | |
title := c.Param("title") | |
c.JSON(200, gin.H{ | |
"id": id, | |
"title": title, | |
}) | |
} | |
func queryHandler(c *gin.Context) { | |
title := c.Query("title") | |
price := c.Query("price") | |
c.JSON(200, gin.H{ | |
"title": title, | |
"price": price, | |
}) | |
} | |
type BookInput struct { | |
Title string `json:"title" binding:"required"` | |
Price int `json:"price" binding:"required|number"` | |
// SubTitle string `json:"sub_title"` | |
} | |
func postBooksHandler(c *gin.Context) { | |
var bookinput BookInput | |
err := c.ShouldBindJSON(&bookinput) | |
if err != nil { | |
c.JSON(400, gin.H{ | |
"error": err.Error(), | |
}) | |
fmt.Println(err) | |
return | |
} | |
c.JSON(200, gin.H{ | |
"title": bookinput.Title, | |
"price": bookinput.Price, | |
// "sub_title": bookinput.SubTitle, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment