4

Go语言网络编程入门不走弯路最佳案例(写Api接口)

 3 years ago
source link: https://studygolang.com/articles/32151
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Go语言是Google领导开发的一门编程语言,国内可访问的官网 https://golang.google.cn/

15bb3246bcc505a6771e92e2c79c4031.png
image-20201213123438844

只要选对了框架,用Go语言完成网络编程会变得非常容易,目前Github Star数量最多的Go语言的框架为gin, 开源地址 github.com/gin-gonic/gin ,并且提供了中文文档 https://gin-gonic.com/zh-cn/docs/

写一个最简单的Get接口

    // 获取路由
    r := gin.Default()

    // 最简单的回应 http://127.0.0.1:8080/ping
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong(最简单的回应)",
        })
    })
76cb5cd3e3d990fcc53b188a07161bf4.png
image-20201213122245906

写一个鹦鹉学舌式的小程序

// GET版 鹦鹉学舌 http://127.0.0.1:8080/message?name=dd
    r.GET("/message", func(c *gin.Context){
        name := c.Query("name");
        data := map[string]interface{}{
            "name": name,
        }
        c.JSON(200, gin.H{
            "status": 1000,
            "message": "响应鹦鹉学舌",
            "data": data,
        })
    })
f385f65f6749f1ccdd4c58c93cbeebe8.png
image-20201213122441082

写一个响应POST 请求的接口

// POST版鹦鹉学舌 http://127.0.0.1:8080/movie

    type Info struct {
        Name string `json:"name"`
        Score int `json:"score"`
    }

    r.POST("/movie", func(c *gin.Context){

        // 以Info为模板初始化data
        var data Info
        
        // 将请求参数绑定到data
        c.BindJSON(&data);

        c.JSON(200, gin.H{
            "status": 1000,
            "message": "返回电影名称和评分",
            "data": data,
        })

    })

5bf0b8d00952d52977d59702a3790e2e.png
image-20201213122620459

以上实例完整代码

使用之前请先运行 go get github.com/gin-gonic/gin 安装依赖包

package main

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

func main() {
    // 获取路由
    r := gin.Default()

    // 最简单的回应 http://127.0.0.1:8080/ping
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong(最简单的回应)",
        })
    })
    
    // GET版 鹦鹉学舌 http://127.0.0.1:8080/message?name=dd
    r.GET("/message", func(c *gin.Context){
        name := c.Query("name");
        data := map[string]interface{}{
            "name": name,
        }
        c.JSON(200, gin.H{
            "status": 1000,
            "message": "响应鹦鹉学舌",
            "data": data,
        })
    })

    // POST版鹦鹉学舌 http://127.0.0.1:8080/movie

    type Info struct {
        Name string `json:"name"`
        Score int `json:"score"`
    }

    r.POST("/movie", func(c *gin.Context){

        // 以Info为模板初始化data
        var data Info
        
        // 将请求参数绑定到data
        c.BindJSON(&data);

        fmt.Println("=data=>>",data);


        c.JSON(200, gin.H{
            "status": 1000,
            "message": "返回电影名称和评分",
            "data": data,
        })

    })


    r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

Go语言编译器安装教程

https://www.runoob.com/go/go-environment.html

Go语言是Google主导开发的语言,拥有C++级别的性能,Python级别的易用度,如果你想写性能极高的程序, 又想保护发际线, 避开C++ , 那Go语言值得一学~

阅读原文(支持读写评论)

https://www.v2fy.com/p/2020-12-13-go/


有疑问加站长微信联系(非本文作者)

280

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK