1. 简单的相应
返回:{"msg":"TEST"}
代码:
package main
import "github.com/gin-gonic/gin"
func main() {
ginServer := gin.Default()
ginServer.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "TEST"})
})
ginServer.Run(":8082")
}
2.带参的相应
2.1传统
请求:http://localhost:8082/hello?username=wenlong&password=wenlong711
结果:{"password":"wenlong711","username":"wenlong"}
代码:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
ginServer := gin.Default()
ginServer.GET("/hello", func(context *gin.Context) {
username := context.Query("username")
password := context.Query("password")
context.JSON(http.StatusOK, gin.H{
"username": username,
"password": password,
})
})
ginServer.Run(":8082")
}
2.2restful方式
结果:{"password":"wenlong711","username":"wenlong"}
代码:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
ginServer := gin.Default()
ginServer.GET("/hello/:username/:password", func(context *gin.Context) {
username := context.Param("username")
password := context.Param("password")
context.JSON(http.StatusOK, gin.H{
"username": username,
"password": password,
})
})
ginServer.Run(":8082")
}
接收JSON
post请求:http://localhost:8082/json
请求体{"password":"wenlong711","username":"wenlong"}
结果:{"password":"wenlong711","username":"wenlong"}
代码
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
ginServer := gin.Default()
ginServer.POST("/json", func(context *gin.Context) {
data,_:=context.GetRawData()
var m map[string]interface{}
_ = json.Unmarshal(data,&m)
context.JSON(http.StatusOK,m)
})
ginServer.Run(":8082")
}
3.数据库操作
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"log"
"net/http"
)
// https://segmentfault.com/a/1190000020411917
type Express struct {
Id int `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
Address string `json:"address"`
Subject string `json:"subject"`
Mail string `json:"mail"`
Date string `json:"date"`
SingleNumber string `json:"single_number"`
}
// 定义一个getALL函数用于回去全部的信息
func getAll(area string) (_express []Express, err error) {
//1.连接数据库
db, err := sql.Open("mysql", "nufe:yWpRE2i3JzfnERcY@(127.0.0.1:3306)/nufe?charset=utf8")
if err != nil {
panic(err)
}
defer db.Close()
if err := db.Ping(); err != nil {
fmt.Println("连接失败")
panic(err)
//return db
}
fmt.Println("连接成功")
//2.查询
rows, err := db.Query("SELECT * FROM express2021_09_27 where address like ?;", "%"+area+"%")
if err != nil {
log.Fatal(err.Error())
}
for rows.Next() {
var express Express
//遍历表中所有行的信息
rows.Scan(&express.Id, &express.Name, &express.Phone, &express.Address, &express.Subject, &express.Mail, &express.Date, &express.SingleNumber)
//将express添加到_express中
_express = append(_express, express)
}
//最后关闭连接
defer rows.Close()
return
}
func main() {
SetupServer().Run(":8082")
}
func SetupServer() *gin.Engine {
r := gin.Default()
r.GET("/list/:area", listHandler)
return r
}
func listHandler(context *gin.Context) {
area := context.Param("area")
_express, err := getAll(area)
if err != nil {
log.Fatal(err)
}
context.JSON(http.StatusOK, gin.H{
"message": _express,
})
}
4.简单的对象封装demo
package main
import "fmt"
type Hero struct {
Name string
Ad int
Level int
}
func (this *Hero) GetName() string {
return this.Name
}
func (this *Hero) Show() {
fmt.Println("Name = ", this.Name)
fmt.Println("Ad = ", this.Ad)
fmt.Println("Level = ", this.Level)
}
func (this *Hero) SetName(newName string) {
this.Name = newName
}
func main() {
hero := Hero{Name: "zhang3", Ad: 100, Level: 1}
hero.Show()
hero.SetName("li4")
hero.Show()
}
import的使用
//hello.go
package hello
import (
"fmt"
)
func SayHello() {
fmt.Println("SayHello()-->Hello")
}
//main.go
package main
import (
"hello"
)
func main() {
hello.SayHello()
}
结构体的使用
package main
import (
"fmt"
)
type Hero struct {
Name string
Ad int
Level int
}
func (this *Hero) GetName() string {
return this.Name
}
func (this *Hero) Show() {
fmt.Println("Name = ", this.Name)
fmt.Println("Ad = ", this.Ad)
fmt.Println("Level = ", this.Level)
}
func (this *Hero) SetName(newName string) {
this.Name = newName
}
func main() {
hero := Hero{Name: "zhang3", Ad: 100, Level: 1}
hero.Show()
hero.SetName("li4")
hero.Show()
}