-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
33 lines (26 loc) · 827 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Defining a struct for albums - json tag is used so that when the struct is converted to json the field names are as mentioned in the double quotes
type album struct {
ID int `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
// Creating data for the struct
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 getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
}
func main() {
router := gin.Default()
router.GET("/albums", getAlbums)
router.Run("localhost:8080")
}