1. 程式人生 > 實用技巧 >通過Goland實現Go語言庫的簡易安裝

通過Goland實現Go語言庫的簡易安裝

一、使用Goland新建專案,選擇Go Modules

二、修改Proxy為"https://goproxy.cn"

三、import所需的庫,並執行(以gin為例)

package main

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

func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() 
}

四、等待Goland後臺自動安裝所需庫完成

GOROOT=C:\Go #gosetup
GOPATH=C:\Users\chenrifa\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\chenrifa\AppData\Local\Temp\___go_build_main_go.exe F:/awesomeProject/main.go #gosetup
go: finding module for package github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.6.3
go: found github.com/gin-gonic/gin in github.com/gin-gonic/gin v1.6.3 go: downloading github.com/gin-contrib/sse v0.1.0 go: downloading github.com/mattn/go-isatty v0.0.12 go: downloading github.com/ugorji/go v1.1.7 go: downloading gopkg.in/yaml.v2 v2.2.8 go: downloading github.com/ugorji/go/codec v1.1.7 go: downloading github.com
/golang/protobuf v1.3.3 go: downloading github.com/go-playground/validator/v10 v10.2.0 go: downloading github.com/go-playground/universal-translator v0.17.0 go: downloading github.com/leodido/go-urn v1.2.0 go: downloading github.com/go-playground/locales v0.13.0 C:\Users\***\AppData\Local\Temp\___go_build_main_go.exe #gosetup

五、可正常執行程式碼

[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    /ping                     --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080