Golang初学:新建项目 及 gin web 项目

go version go1.22.1 windows/amd64

VS code 1.89.0

---

 

序章

建立一个 web项目,使用 gin web 框架。

 

Tutorial: Developing a RESTful API with Go and Gin
https://go.dev/doc/tutorial/web-service-gin

the Gin Web Framework (Gin).
https://gin-gonic.com/docs/

 

步骤

创建、打开、初始化

建立 空白文件夹 gin001。

VS code 打开 gin001。

打开 VS code 的 终端:Ctrl + `(左上角 ESC 下面)。ben发布于博客园

终端 默认会 进入项目 gin001 的根目录:

 

执行命令 初始化模块:

> go mod init gin001
go: creating new go.mod: module gin001

变化:多了一个 go.mod 文件

 

安装 gin

执行下面的命令,结果,安装了好多东西(包),go.mod 也出现了很大的变化,还都了一个 go.sum 文件。ben发布于博客园

PS D:\code\0.gitea\gin001> go get github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.10.0
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.20
go: downloading golang.org/x/net v0.25.0
go: downloading github.com/bytedance/sonic v1.11.6
go: downloading github.com/goccy/go-json v0.10.2
go: downloading github.com/json-iterator/go v1.1.12
go: downloading github.com/go-playground/validator/v10 v10.20.0
go: downloading github.com/pelletier/go-toml/v2 v2.2.2
go: downloading github.com/ugorji/go/codec v1.2.12
go: downloading google.golang.org/protobuf v1.34.1
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading golang.org/x/sys v0.20.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading github.com/gabriel-vasile/mimetype v1.4.3
go: downloading github.com/go-playground/universal-translator v0.18.1
go: downloading github.com/leodido/go-urn v1.4.0
go: downloading golang.org/x/crypto v0.23.0
go: downloading golang.org/x/text v0.15.0
go: downloading github.com/go-playground/locales v0.14.1
go: downloading github.com/cloudwego/base64x v0.1.4
go: downloading golang.org/x/arch v0.8.0
go: downloading github.com/bytedance/sonic/loader v0.1.1
go: downloading github.com/twitchyliquid64/golang-asm v0.15.1
go: downloading github.com/klauspost/cpuid/v2 v2.2.7
go: downloading github.com/cloudwego/iasm v0.2.0
go: added github.com/bytedance/sonic v1.11.6
go: added github.com/bytedance/sonic/loader v0.1.1
go: added github.com/cloudwego/base64x v0.1.4
go: added github.com/cloudwego/iasm v0.2.0
go: added github.com/gabriel-vasile/mimetype v1.4.3
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.10.0
go: added github.com/go-playground/locales v0.14.1
go: added github.com/go-playground/universal-translator v0.18.1
go: added github.com/go-playground/validator/v10 v10.20.0
go: added github.com/goccy/go-json v0.10.2
go: added github.com/json-iterator/go v1.1.12
go: added github.com/klauspost/cpuid/v2 v2.2.7
go: added github.com/leodido/go-urn v1.4.0
go: added github.com/mattn/go-isatty v0.0.20
go: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: added github.com/modern-go/reflect2 v1.0.2
go: added github.com/pelletier/go-toml/v2 v2.2.2
go: added github.com/twitchyliquid64/golang-asm v0.15.1
go: added github.com/ugorji/go/codec v1.2.12
go: added golang.org/x/arch v0.8.0
go: added golang.org/x/crypto v0.23.0
go: added golang.org/x/net v0.25.0
go: added golang.org/x/sys v0.20.0
go: added golang.org/x/text v0.15.0
go: added google.golang.org/protobuf v1.34.1
go: added gopkg.in/yaml.v3 v3.0.1

go.mod 内容

go.sum 文件

建立 main.go 文件

package main

import "fmt"

func main() {
	fmt.Println("start main...")
}

执行:

PS D:\code\0.gitea\gin001> go run .
start main...

ben发布于博客园

开发 gin web 项目

参考 官方的 Tutorial: Developing a RESTful API with Go and Gin 开发 仅1个GET接口 的项目。

端口:40000

改造后的 main.go:

package main

import (
	"fmt"
	"net/http"

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

const gport = 40000

func main() {
	fmt.Println("start main...")

	router := gin.Default()

	router.GET("/api/001", handler001)

	addr := fmt.Sprintf("localhost:%d", gport)
	router.Run(addr)
}

// 返回 json 数据
func handler001(c *gin.Context) {
	resp := map[string]int{
		"apple":  1,
		"banana": 2,
		"orange": 3,
	}

	c.IndentedJSON(http.StatusOK, resp)
}

运行:

PS D:\code\0.gitea\gin001> go run .
start main...
[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    /api/001                  --> main.handler001 (3 handlers)
[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 localhost:40000

浏览器访问:

http://localhost:40000/

返回:

404 page not found

--未定义接口,合理

 

http://localhost:40000/api/001

返回:

--已定义接口,正常。

 

日志可以看到请求详情:

居然,精确到 微秒(us)

ben发布于博客园

编译后运行

执行  go build 命令。

项目下 生成 gin001.exe 可执行文件。

可执行文件大小:10MB 左右。

执行 gin001.exe:

浏览器访问:正常。

ben发布于博客园

本文用到的命令

  1. Ctrl + `
    1. VS code 打开终端或关闭
  2. go mod init gin001
  3. go get github.com/gin-gonic/gin
  4. go run .
  5. go build

 

🚩

 

补充,后续可以 打包到 Linux 主机进行测试(交叉编译 可以看 自己 前面一篇博文)。

 

---END---

 

本文链接:

https://www.cnblogs.com/luo630/p/18181639

ben发布于博客园

参考资料

1、

 

ben发布于博客园

ben发布于博客园

 

posted @ 2024-05-09 12:12  快乐的欧阳天美1114  阅读(202)  评论(0编辑  收藏  举报