Golang初學:新建專案 及 gin web 專案

快乐的凡人721發表於2024-05-09

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。

Golang初學:新建專案 及 gin web 專案

開啟 VS code 的 終端:Ctrl + `(左上角 ESC 下面)。ben釋出於部落格園

終端 預設會 進入專案 gin001 的根目錄:

Golang初學:新建專案 及 gin web 專案

執行命令 初始化模組:

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

變化:多了一個 go.mod 檔案

Golang初學:新建專案 及 gin web 專案

安裝 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 內容

Golang初學:新建專案 及 gin web 專案

go.sum 檔案

Golang初學:新建專案 及 gin web 專案

建立 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

返回:

Golang初學:新建專案 及 gin web 專案

--已定義介面,正常。

日誌可以看到請求詳情:

Golang初學:新建專案 及 gin web 專案

居然,精確到 微秒(us)

ben釋出於部落格園

編譯後執行

執行 go build 命令。

專案下 生成 gin001.exe 可執行檔案。

Golang初學:新建專案 及 gin web 專案

可執行檔案大小:10MB 左右。

Golang初學:新建專案 及 gin web 專案

執行 gin001.exe:

Golang初學:新建專案 及 gin web 專案

瀏覽器訪問:正常。

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釋出於部落格園

相關文章