getting started with gin framewrok

  • Using the gin framework,return string types,JSON types,XML types ,struct types,JSONP type data.
  • the use of template and template syntax
  • Access mysql using the ORM package

Using the gin framework,return string types,

JSON types,XML types ,struct types,JSONP type data.

(The go runtime environment has been installed by default)

#create a main.go file

package main 

func main(){


}

###########Expand content

------------------------------------------------------------------------------------------------------------------------------

#First we need to understand the golang packages mangement commands:go mod

1.go mod help                  //view help
2.go mod init project name    //initializaiton module,which generates a go.mod file in the root directory of the project
3.go mod tidy                //handles dependencies according to the gom.mod file
4,go list -m all            //show dependencies
5.go list -m -json all     //show detailed dependencies

--------------------------------------------------------------------------------------------------------------------------------

#initialize the project and generate go.mod file in the project root directory

go mod init project name

#To install Gin package

go get -u github.com/gin-gonic/gin

#pull missing modules and remove unnecessary modules

go mod tidy

#main.go

#Write helloworld programs based on gin framework

package main


import (
	"github.com/gin-gonic/gin"
)

func main() {
	//create a route with default engine
	r := gin.Default()
	//get:request method;"/":request path
	//when the client request / path with the get method,the following  anonymous function is executed.
	r.GET("/", func(c *gin.Context) {
		//c.String,returns data in string format
		c.String(200, "value: %v", "Home page")
	})
	//start the HTTP service.By default,start the service at 0.0.0.0:8080
	//modify the server external service port to 9000
	r.Run(":9000")
}

#Run the code

go run main.go

    Execution result:    Listening and serving HTTP on :9000

curl http://localhost:9000

    Execution result:    value: Home pageusr

###########Expand content

--------------------------------------------------------------------------------------------------------------------------------

#The method of creating router in the gin framework

r := gin.Default()   //create a route with default middle

r := gin.new()      //create a route without middleware

#Rotuting request mode

r.GET("/someGet",getting)

r.POST("/somePost",posting)

r.PUT("/somePut",putting)

r.Delete("/someDelete",eleting)

r.PATCH("somePatch",patching)

r.HEAD("/someHead",head)

r.OPTIONS("/someOptions",options)

---------------------------------------------------------------------------------------------------------------------------------

#Use golang native writing to return the JSON data type

func main(){
    //create a route with default engine
    r:=gin.Default()    
    //GET:request method,'/':request path
    r.GET("/json",func(c *gin.Context){
        //c.JSON:returns the JSON data type
        c.JSON(200,map[string]interface{}{
            "success":true,
            "msg":"hello gin,this is an json data",
        })
    })
go run main.go   

//Execute result:  Listening and serving HTTP on :8080

curl http://localhost:8080/json

//Execute result:  {"msg":"hello gin,this is an json data","success":true}

#Use gin.H return json data

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	//Using gi.H in the gin framework,it actually implements an empty interface type with the same effect as above.
	r.GET("/json2", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"success": true,
			"msg":     "hello gin,this is an json data",
		})
	})
	r.Run()
}
go run main.go

//Execute result:
//Listening and serving HTTP on :8080
//[GIN] 2022/11/03 - 10:54:08 | 200 |     132.192µs |       127.0.0.1 | GET      //"/json2"

curl htpp://localhost:8080/json2

//Execute result:
//{"msg":"hello gin,this is an json data","success":true}

#Use gin.H return jsonp data???????

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	r.GET("/jsonp", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"success": true,
			"msg":     "hello gin,this is an json data",
		})
	})
	r.Run()
}
go run main.go

//Execute result:
// Listening and serving HTTP on :8080
//[GIN] 2022/11/03 - 10:59:36 | 200 |      98.303µs |       127.0.0.1 | GET      //"/jsonp"


curl http://localhost:8080/jsonp

//Execute result:
//{"msg":"hello gin,this is an json data","success":true}

#Return xml data

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	r.GET("/xml", func(c *gin.Context) {
		c.XML(http.StatusOK, gin.H{
			//	c.XML(http.StatusOK,map[string]interface{}{
			"success": true,
			"msg":     "return xml data",
		})
	})
	r.Run(":8088")
}

#Execute:

go run main.go

//Execute result:
//Listening and serving HTTP on :8088
//[GIN] 2022/11/03 - 11:13:12 | 200 |     113.533µs |       127.0.0.1 | GET      //"/xml"

curl http://localhost:8088/xml

//Execute result:
//<map><success>true</success><msg>return xml data</msg></map>

#Return struct data

package main

import (
	"github.com/gin-gonic/gin"
)

type Article struct {
	Title   string `json:"title"`
	Desc    string `json:"desc"`
	Content string `json:"content"`
}

func main() {
	r := gin.Default()
	//First ,we define a structure data type,Article.
	r.GET("/stru", func(c *gin.Context) {
		//Initialize structure data
		a := &Article{
			Title:   "thi is a title",
			Desc:    "this is a description",
			Content: "this is an test content",
		}
		c.JSON(200, a)
	})
	r.Run(":8088")
}

#Execute:

go run main.go

//Execute result:
// Listening and serving HTTP on :8088
//[GIN] 2022/11/03 - 11:17:15 | 200 |     378.085µs |       127.0.0.1 | GET      //"/stru"

curl http://localhost:8088/stru

//Execute result:
//{"title":"thi is a title","desc":"this is a description","content":"this is an //test content"}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值