当前位置: 首页 > news >正文

Go Web--Go Module

目录

一、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=onC:\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_modelD:\GO_workspace_web>cd test_modelD:\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_modelgo 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_modelgo 1.20require (github.com/bytedance/sonic v1.10.0 // indirectgithub.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirectgithub.com/chenzhuoyu/iasm v0.9.0 // indirectgithub.com/gabriel-vasile/mimetype v1.4.2 // indirectgithub.com/gin-contrib/sse v0.1.0 // indirectgithub.com/gin-gonic/gin v1.9.1 // indirectgithub.com/go-playground/locales v0.14.1 // indirectgithub.com/go-playground/universal-translator v0.18.1 // indirectgithub.com/go-playground/validator/v10 v10.15.0 // indirectgithub.com/goccy/go-json v0.10.2 // indirectgithub.com/json-iterator/go v1.1.12 // indirectgithub.com/klauspost/cpuid/v2 v2.2.5 // indirectgithub.com/leodido/go-urn v1.2.4 // indirectgithub.com/mattn/go-isatty v0.0.19 // indirectgithub.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirectgithub.com/modern-go/reflect2 v1.0.2 // indirectgithub.com/pelletier/go-toml/v2 v2.0.9 // indirectgithub.com/twitchyliquid64/golang-asm v0.15.1 // indirectgithub.com/ugorji/go/codec v1.2.11 // indirectgolang.org/x/arch v0.4.0 // indirectgolang.org/x/crypto v0.12.0 // indirectgolang.org/x/net v0.14.0 // indirectgolang.org/x/sys v0.11.0 // indirectgolang.org/x/text v0.12.0 // indirectgoogle.golang.org/protobuf v1.31.0 // indirectgopkg.in/yaml.v3 v3.0.1 // indirect
)

编写main.go

package mainimport ("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 cacheedit        edit go.mod from tools or scriptsgraph       print module requirement graphinit        initialize new module in current directorytidy        add missing and remove unused modulesvendor      make vendored copy of dependenciesverify      verify dependencies have expected contentwhy         explain why packages or modules are neededUse "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 graphD:\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 verifyD:\GO_workspace_web\test_model>go mod verify
all modules verified

查找依赖

go mod whyD:\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学习+深入(一)

干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!

http://www.lryc.cn/news/121505.html

相关文章:

  • Spring Boot 统一功能处理(拦截器实现用户登录权限的统一校验、统一异常返回、统一数据格式返回)
  • P4058 [Code+#1] 木材
  • Python学习笔记第五十二天(Pandas 安装)
  • 分布式搜索ElasticSearch-ES(一)
  • react学习笔记——3. jsx语法规则
  • MySQL分表实现上百万上千万记录分布存储的批量查询设计模式
  • 射频入门知识-1
  • 基于注解函数式编程实现组件解耦设计
  • 并查集、树状数组
  • ES6中Null判断运算符(??)正确打开方式-
  • java的内存模型
  • 基于 CentOS 7 构建 LVS-DR 群集 配置nginx负载均衡
  • CSS练习
  • 基于深度学习的3D城市模型增强【Mask R-CNN】
  • LabVIEW对并行机器人结构进行建模仿真
  • 【算法题】1281. 整数的各位积和之差
  • (一)ES6 介绍
  • 窥孔优化(Peephole Optimization)
  • Docker安装ElasticSearch/ES 7.4.0
  • 无涯教程-Perl - readline函数
  • 类与对象(入门)
  • 刷题记录(2023-08-12)
  • GPT内功心法:搜索思维到GPT思维的转换
  • 在WebStorm中通过live-server插件搭建Ajax运行环境
  • 侯捷 C++ part2 兼谈对象模型笔记——1 转换
  • 尚硅谷大数据项目《在线教育之采集系统》笔记003
  • PAT(Advanced Level)刷题指南 —— 第七弹
  • 合宙Air724UG LuatOS-Air script lib API--sys
  • MySQL建表和增添改查
  • @Transactional 注解下,事务失效的七种场景