世界上最伟大的投资就是投资自己的教育

全场限时 5 折

首页Golang
随风 · 练气

前端程序员学习 Golang gin 框架实战笔记之四 struct tag

随风发布于3857 次阅读

上一节:前端程序员学习 Golang gin 框架实战笔记之三 panic 和 recovery

我们总要返回出 json 吧:

package main

import (
    "net/http"

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

type Book struct {
    ID     string `json:"id"`
    Title  string `json:"title"`
    Author string `json:"author"`
}

var books = []Book{
    {ID: "1", Title: "Harry Potter", Author: "J. K. Rowling"},
    {ID: "2", Title: "The Lord of the Rings", Author: "J. R. R. Tolkien"},
    {ID: "3", Title: "The Wizard of Oz", Author: "L. Frank Baum"},
}

func main() {
    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.JSON(http.StatusOK, books)
    })
    r.Run()
}

本站文章均为原创内容,如需转载请注明出处,谢谢。

0 条回复
暂无回复~~
喜欢
统计信息
    学员: 29232
    视频数量: 1985
    文章数量: 489

© 汕尾市求知科技有限公司 | Rails365 Gitlab | Qiuzhi99 Gitlab | 知乎 | b 站 | 搜索

粤公网安备 44152102000088号粤公网安备 44152102000088号 | 粤ICP备19038915号

Top