[Golang] Gin & Goland & go.mod 快速建立

打開Goland後main.go檔內複製下方的Code:

package main

import (
    "github.com/gin-gonic/gin"
)
func main() {
    app := gin.Default()
    app.GET("/hello/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.JSON(200, gin.H{
            "message": "hello " + name,
        })
    })
    err := app.Run(":8080")
    if err != nil {
        panic(err)
    }
}

會看到很多紅字如下圖

打開Goland的file -> settings -> Go Modules ->
將Enable Go modules integration打勾

再Goland下方打開terminal,輸入

$ go mod init [project_name]
$ go mod tidy

此時的go.mod會自動下載

module do_gowork

go 1.19

require github.com/gin-gonic/gin v1.8.1

require (
    github.com/gin-contrib/sse v0.1.0 // indirect
    github.com/go-playground/locales v0.14.0 // indirect
    github.com/go-playground/universal-translator v0.18.0 // indirect
    github.com/go-playground/validator/v10 v10.10.0 // indirect
    github.com/goccy/go-json v0.9.7 // indirect
    github.com/json-iterator/go v1.1.12 // indirect
    github.com/leodido/go-urn v1.2.1 // indirect
    github.com/mattn/go-isatty v0.0.14 // indirect
    github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
    github.com/modern-go/reflect2 v1.0.2 // indirect
    github.com/pelletier/go-toml/v2 v2.0.1 // indirect
    github.com/ugorji/go/codec v1.2.7 // indirect
    golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
    golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
    golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
    golang.org/x/text v0.3.6 // indirect
    google.golang.org/protobuf v1.28.0 // indirect
    gopkg.in/yaml.v2 v2.4.0 // indirect
)

注意,go mod tidy有移除「沒使用的依賴這功能」,實際上是需要使用來取得library。

go get [library]

空白處點選右鍵,Run ‘go build main.go’

執行成功可看到下面這些訊息

[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    /hello/:name              --> main.main.func1 (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 :8080

打開瀏覽器輸入:
http://127.0.0.1:8080/hello/world

{“message”:”hello world”}

發佈留言