
2024年七大最佳免費(fèi)貨幣轉(zhuǎn)換API
json:"id"
Title string json:"title"
Artist string json:"artist"
Price float64 json:"price"
}
// albums slice to seed record album data.
var albums = []album{
{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
}
func main() {
router := gin.Default()
router.GET("/albums", getAlbums)
router.GET("/albums/:id", getAlbumByID)
router.POST("/albums", postAlbums)
router.Run("localhost:8080")
}
// getAlbums responds with the list of all albums as JSON.
func getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
}
// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
var newAlbum album
// Call BindJSON to bind the received JSON to
// newAlbum.
if err := c.BindJSON(&newAlbum); err != nil {
return
}
// Add the new album to the slice.
albums = append(albums, newAlbum)
c.IndentedJSON(http.StatusCreated, newAlbum)
}
// getAlbumByID locates the album whose ID value matches the id
// parameter sent by the client, then returns that album as a response.
func getAlbumByID(c *gin.Context) {
id := c.Param("id")
// Loop through the list of albums, looking for
// an album whose ID value matches the parameter.
for _, a := range albums {
if a.ID == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "album not found"})
}
原文鏈接:https://go.dev/doc/tutorial/web-service-gin
2024年七大最佳免費(fèi)貨幣轉(zhuǎn)換API
如何通過Smart Image Cropping API自動裁剪圖像?
News API + React:創(chuàng)建一個卓越的實(shí)時新聞應(yīng)用程序
30款免費(fèi)開放的API,助力營銷人員與內(nèi)容開發(fā)者
免費(fèi)獲取韻達(dá)快遞查詢API的使用指南
OpenAI ChatGPT API 與 React JS 的完美結(jié)合:全面指南
面向營銷人員的 API:前 7 名免費(fèi) REST API
常用文檔轉(zhuǎn)換API匯總
2024年國內(nèi)熱門天氣環(huán)境API