【Go官方文档】Developing a RESTful API with Go and Gin

环境配置:

  • 系统:Windows11
  • 编辑器:vscode

🍐 创建项目

  1. 创建项目文件夹web-service-gin并用vscode打开该项目
  2. 初始化go mod
1
go mod init example/web-service-gin
  1. 创建文件main.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"net/http"

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

type album struct {
	ID     string  `json:"id"`
	Title  string  `json:"title"`
	Artist string  `json:"artist"`
	Price  float64 `json:"price"`
}

var albums = []album{
	{ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99},
	{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
	{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
}

func getAlbums(c *gin.Context) {
	c.IndentedJSON(http.StatusOK,albums)
}

func main() {
	router := gin.Default()
	router.GET("albums",getAlbums)

	router.Run("localhost:8080")
}

🥝 运行项目

  1. 执行命令:
1
go run .
  1. 打开浏览器,地址栏输入:http://localhost:8080/albums,回车确定
  2. 浏览器显示内容:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[
    {
        "id": "1",
        "title": "Blue Train",
        "artist": "John Coltrane",
        "price": 56.99
    },
    {
        "id": "2",
        "title": "Jeru",
        "artist": "Gerry Mulligan",
        "price": 17.99
    },
    {
        "id": "3",
        "title": "Sarah Vaughan and Clifford Brown",
        "artist": "Sarah Vaughan",
        "price": 39.99
    }
]
0%