GIN is a simple type of the https get post pagekage
Here let us start to use the gin . It 's really simple to use it. But I need to pretend there are the chare to see my blog. So let me start from the begining .
Before we say about the gin we need to find out what is the api and https.
API (Application Programming Interface)
It's the four way computer comunication information (data) to each other.
Get: get data or infomation from someone.
Post:post data from someone need to put in my data ,this data is a brand new data. Didn't exist before(we said create the new data in daily)
Put: put means to switch the data,edit the data already exist.(we said update the data in usually)
Delete: Really simple is to delet the data no need to explaintion.
We allways use json formate our data
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
HTTPS(HyperText Transfer Protocol Secure)
As we says that it's a Protocol of Transfer type.Is hard to means. I use a simple way to say is a contract of data transfer.
Gin
After you know what is what is the api and https. Here is time to let me to introducing the Gin .
Gin is a package use the Https to build api.
Simple build :Quickstart
package main
import "github.com/gin-gonic/gin"
type Blance struct {
ID string `json:"id"`
}
var Blance = []album{
{ID: "2"}
}
func getBalance(c *gin.Context) {
c.IndentedJSON(http.StatusOK, Blance)
}
func main() {
router := gin.Default()
router.GET("/getBalance", getBalance)
// you may able to abb
router.SetTrustedProxies([]string{"192.168.1.1"}) // setting trust
router.Run(":8080")// setting Port
}
Build up a simple api by gin
You tpye on you browser https://192.168.1.1:8080/getBalance
It will return the id:2
Advance Skill(Complexe Structure)
Json Build (only one main at once in your code )
package main
import "github.com/gin-gonic/gin"
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
}
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},
}
// c *gin.Context pointer gin
func getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
}
func postAlbums(c *gin.Context) {
var newAlbum album // newAlbum .
// Call BindJSON to bind the received JSON to
if err := c.BindJSON(&newAlbum); err != nil {
return
}
// Add the new album to the slice.
albums = append(albums, newAlbum)
c.IndentedJSON(http.StatusCreated, newAlbum)
}
func getAlbumByID(c *gin.Context) {
id := c.Param("id") // parameter will add
// 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"})
}
//
func main() {
router := gin.Default()
router.GET("/albums", getAlbums)
router.GET("/albums/:id", getAlbumByID)
router.POST("/albums", postAlbums)
router.Run("localhost:8080")
}
Postman do command
use the post way
{"id": "4","title": "The Modern Sound of Betty Carter","artist": "Betty Carter","price": 49.99}
The data will add to the back-end but it's temp data in cahche. When you reload the treminal the cached data will clean up
Where is the delet and put
package main
import "github.com/gin-gonic/gin"
type data struct {
ID string `json:"id"`
}
func update_id(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{
"id": 2,
})
}
func delet_id(c *gin.Context) {
id := c.Param("id")
c.IndentedJSON(http.StatusOK, gin.H{
"id": id,
})
}
func getdata (c *gin.Context){
var sample = []data
c.IndentedJSON(http.StatusOK,
sample
}
}
func main() {
router := gin.Default()
r.get("/path",getdata)
r.POST("/path/:id",update_id)
r.DELETE("/path/:id",delet_id)
}