http get

package main

import (
    "fmt"
    "net/http"
)

func dealGetHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello golang!"))
}
func main() {
    http.HandleFunc("/req/get", dealGetHandler)
    fmt.Println("http://127.0.0.1:8000/req/get")
    http.ListenAndServe(":8000", nil)

}

http server

client http post

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "strings"
)

func main()  {
    url := "http://localhost:8090/req/post"
    contentType := "application/json"
    data2 := `{
        "Name":"root",
        "Passwd":"1234",
        "Age": 24
    }`

    resp,_ := http.Post(url,contentType,strings.NewReader(data2))
    b,_ := ioutil.ReadAll(resp.Body)
    fmt.Println(b)
}

gin

package main

import (
    "fmt"

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

func main() {
    // 1、创建路由engine
    // r就是 *Engine 结构体
    r := gin.Default()
    // 2、路由绑定
    /*
        "/" 路由
        func(c *gin.Context)  处理函数
    */
    r.GET("/", func(c *gin.Context) {
        // 第一:解析get/post请求的参数
        // 第二:根据参数去查询数据库(操作数据库:添加数据、删除数据)
        // 第三:返回从数据库查询的数据
        c.String(200, "hello golang~")
    })
    fmt.Println("http://127.0.0.1:8000")
    // 3、启动监听端口
    // 对 net/http服务的封装,替换了 http.ListenAndServe(address, engine)
    r.Run(":8000")
}


[root@VM-4-12-centos helloword]# go run main.go 
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /                         --> main.main.func1 (3 handlers)
http://127.0.0.1:8000
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8000
[GIN] 2022/05/23 - 14:57:06 | 200 |      74.081µs | 203.205.141.117 | GET      "/"
[GIN] 2022/05/23 - 14:57:07 | 404 |       2.166µs | 203.205.141.117 | GET      "/favicon.ico"
[GIN] 2022/05/23 - 14:57:21 | 200 |      11.325µs | 203.205.141.117 | GET      "/"
[GIN] 2022/05/23 - 14:57:23 | 200 |       9.484µs | 203.205.141.117 | GET      "/"
[GIN] 2022/05/23 - 14:57:25 | 200 |      10.068µs | 203.205.141.117 | GET      "/"
[GIN] 2022/05/23 - 14:57:26 | 200 |      11.043µs | 203.205.141.117 | GET      "/"
[GIN] 2022/05/23 - 14:57:27 | 200 |      28.942µs | 203.205.141.117 | GET      "/"

gin server

http file server

package main

import (
    "log"
    "net/http"
)

func main() {
    fs := http.FileServer(http.Dir("./"))
    log.Fatal(http.ListenAndServe(":8080", fs))
}

http server

gin api demo

package main

// API demo
import (
    "fmt"

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

func main() {
    r := gin.Default()
    r.GET("/book/:id", GetBookDealHandler)
    r.Run(":8080")
}

func GetBookDealHandler(b *gin.Context) {
    bookID := b.Param("id")
    fmt.Println(bookID, "____>")
    b.String(200, "API params")
}

postman get gin api demo

gin url

package main

import (
    "fmt"

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

func main() {
    r := gin.Default()
    r.GET("/user", UserDetailHandler)
    r.Run(":8080")
}

func UserDetailHandler(u *gin.Context) {
    name := u.Query("name")
    name2 := u.DefaultQuery("name", "default val")
    fmt.Println("获取的用户名字是:——————", name, name2)
    u.String(200, "url params")
}

go Run postman url

shouldbind POST

package main

import (
    "fmt"

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

type Login struct {
    Username string `json:"username"`
    Password string `json:"password"`
}

func LoginHandler(l1 *gin.Context) {
    var login Login
    if err := l1.ShouldBind(&login); err != nil {
        l1.String(200, "参数校验错误")
    }
    fmt.Println(login.Username, login.Password)
    l1.String(200, "success")
}
func main() {
    r := gin.Default()
    //r.GET("/login/", LoginHandler)
    r.POST("/login/", LoginHandler)
    r.Run(":8090")
}

postman shoulbind post shouldbind post run debug