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發佈於博客園

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章