目录
一、Go Module
1、开启Go Module
2、Go Module基本操作
3、使用GoLand创建Go Module项目
4、GoLand配置File Watchers
一、Go Module
Go Module包管理工具----相当于Maven
1.11版本引入
1.12版本正式支持
告别GOPATH,使用Go Module管理项目,就不需要非得把项目放到GOPATH SRC目录下,你可以在你磁盘的任何位置新建一个项目
C:\Users\Administrator>go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Administrator\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Administrator\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.6
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build3162599993=/tmp/go-build -gno-record-gcc-switches
1、开启Go Module
1、修改环境变量
C:\Users\Administrator>go env -w GO111MODULE=on
C:\Users\Administrator>go env -w GOPROXY=https://goproxy.cn,direct
#GO111MODULE 三种值
GO111MODULE=off go命令行将不会支持module功能,寻找依赖包的方式将会沿用旧版本那种
通过vendor目录或GOPATH模式来查找
GO111MODULE=on go命令将会使用module,也不会取GOPATH目录下查找
GO111MODULE=auto 默认值,go命令将会根据当前目录来决定是否启用module功能
2、命令行方式创建Go Module项目
mkdir projectname
cd projectname
go mod init projectname
#使用GoLand打开工程
#===========S 演示=======
D:\GO_workspace_web>mkdir test_model
D:\GO_workspace_web>cd test_model
D:\GO_workspace_web\test_model>go mod init test_model
go: creating new go.mod: module test_model
#此时test_model下会创建go.mod文件
#使用GoLand打开工程
#===========E 演示=======
go.mod内容
module test_model
go 1.20
在GoLand软件的Terminal中输入
go get -u github.com/gin-gonic/gin
#执行信息如下
PS D:\GO_workspace_web\test_model> go get -u github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.9.1
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading golang.org/x/net v0.10.0
go: downloading github.com/mattn/go-isatty v0.0.19
go: downloading github.com/pelletier/go-toml/v2 v2.0.8
go: downloading github.com/go-playground/validator/v10 v10.14.0
go: downloading github.com/ugorji/go/codec v1.2.11
go: downloading google.golang.org/protobuf v1.30.0
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/bytedance/sonic v1.9.1
go: downloading github.com/pelletier/go-toml v1.9.5
go: downloading github.com/goccy/go-json v0.10.2
go: downloading github.com/pelletier/go-toml/v2 v2.0.9
go: downloading github.com/go-playground/validator/v10 v10.15.0
go: downloading github.com/ugorji/go v1.2.11
go: downloading github.com/json-iterator/go v1.1.12
go: downloading github.com/go-playground/validator v9.31.0+incompatible
go: downloading golang.org/x/sys v0.8.0
go: downloading google.golang.org/protobuf v1.31.0
go: downloading golang.org/x/net v0.14.0
go: downloading github.com/bytedance/sonic v1.10.0
go: downloading github.com/gabriel-vasile/mimetype v1.4.2
go: downloading github.com/go-playground/universal-translator v0.18.1
go: downloading github.com/leodido/go-urn v1.2.4
go: downloading golang.org/x/crypto v0.9.0
go: downloading golang.org/x/text v0.9.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 golang.org/x/sys v0.11.0
go: downloading github.com/go-playground/locales v0.14.1
go: downloading golang.org/x/crypto v0.12.0
go: downloading github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311
go: downloading golang.org/x/text v0.12.0
go: downloading golang.org/x/arch v0.3.0
go: downloading github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d
go: downloading github.com/twitchyliquid64/golang-asm v0.15.1
go: downloading golang.org/x/arch v0.4.0
go: downloading github.com/klauspost/cpuid/v2 v2.2.4
go: downloading github.com/klauspost/cpuid/v2 v2.2.5
go: downloading github.com/klauspost/cpuid v1.3.1
go: downloading github.com/chenzhuoyu/iasm v0.9.0
go: added github.com/bytedance/sonic v1.10.0
go: added github.com/twitchyliquid64/golang-asm v0.15.1
go: added github.com/ugorji/go/codec v1.2.11
go: added golang.org/x/arch v0.4.0
go: added golang.org/x/crypto v0.12.0
go: added golang.org/x/net v0.14.0
go: added golang.org/x/sys v0.11.0
go: added golang.org/x/text v0.12.0
go: added google.golang.org/protobuf v1.31.0
go: added gopkg.in/yaml.v3 v3.0.1
PS D:\GO_workspace_web\test_model>
此时的go.mod内容
module test_model
go 1.20
require (
github.com/bytedance/sonic v1.10.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
编写main.go
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/test", func(c *gin.Context) {
c.JSON(200, gin.H{
"msg": "test success",
})
})
r.Run()
}
运行
访问测试http://localhost:8080/test
2、Go Module基本操作
D:\GO_workspace_web\test_model>go mod help #查看帮助
Go mod provides access to operations on modules.
Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.
Usage:
go mod <command> [arguments]
The commands are:
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
Use "go help mod <command>" for more information about a command.
初始化一个module,模块名为项目名
go mod init projectname
下载module到本地cache,路径为go env中的GOMODCACHE
go mod download
通过工具或脚本剪辑go.mod文件,选项有-json、-require和-exclude可以使用帮助go help mod edit
D:\GO_workspace_web\test_model>go help mod edit
usage: go mod edit [editing flags] [-fmt|-print|-json] [go.mod]
Edit provides a command-line interface for editing go.mod,
for use primarily by tools or scripts. It reads only go.mod;
it does not look up information about the modules involved.
By default, edit reads and writes the go.mod file of the main module,
but a different target file can be specified after the editing flags.
The editing flags specify a sequence of editing operations.
The -fmt flag reformats the go.mod file without making other changes.
This reformatting is also implied by any other modifications that use or
rewrite the go.mod file. The only time this flag is needed is if no other
flags are specified, as in 'go mod edit -fmt'.
The -module flag changes the module's path (the go.mod file's module line).
The -require=path@version and -droprequire=path flags
add and drop a requirement on the given module path and version.
Note that -require overrides any existing requirements on path.
These flags are mainly for tools that understand the module graph.
Users should prefer 'go get path@version' or 'go get path@none',
which make other go.mod adjustments as needed to satisfy
constraints imposed by other modules.
The -exclude=path@version and -dropexclude=path@version flags
add and drop an exclusion for the given module path and version.
Note that -exclude=path@version is a no-op if that exclusion already exists.
The -replace=old[@v]=new[@v] flag adds a replacement of the given
module path and version pair. If the @v in old@v is omitted, a
replacement without a version on the left side is added, which applies
to all versions of the old module path. If the @v in new@v is omitted,
the new path should be a local module root directory, not a module
path. Note that -replace overrides any redundant replacements for old[@v],
so omitting @v will drop existing replacements for specific versions.
......
以文本模式打印模块需求图
go mod graph
D:\GO_workspace_web\test_model>go mod graph #在项目文件夹下执行
test_model github.com/bytedance/sonic@v1.10.0
test_model github.com/chenzhuoyu/base64x@v0.0.0-20230717121745-296ad89f973d
test_model github.com/chenzhuoyu/iasm@v0.9.0
test_model github.com/gabriel-vasile/mimetype@v1.4.2
test_model github.com/gin-contrib/sse@v0.1.0
test_model github.com/gin-gonic/gin@v1.9.1
test_model github.com/go-playground/locales@v0.14.1
test_model github.com/go-playground/universal-translator@v0.18.1
test_model github.com/go-playground/validator/v10@v10.15.0
....
添加缺失或者删除没有使用的modules
go mod tidy
在本地生成vendor目录(旧方式)
go mod vendor
验证依赖是否正确
go mod verify
D:\GO_workspace_web\test_model>go mod verify
all modules verified
查找依赖
go mod why
D:\GO_workspace_web\test_model>go mod why
go: downloading github.com/stretchr/testify v1.8.4
go: downloading github.com/google/go-cmp v0.5.5
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/go-playground/assert/v2 v2.2.0
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
go: downloading github.com/pmezard/go-difflib v1.0.0
# test_model
test_model
3、使用GoLand创建Go Module项目
1、File--->New--->Project....
有Go modules就选择,没有就选择Go
点击Create创建,会自动创建go.mod文件,如下图
其他操作和上面命令行那种方式一样的操作即可。
4、GoLand配置File Watchers
File--->Settings-->Tools--->File Watchers,选择+按照如下配置go fmt和goimports
Golang学习+深入(一)
干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!