go modules 学习

go modules 学习

tags:golang

安装

只需要golang的版本是1.11及之后的,这个模块就内置好了

环境变量

(1) 配置GoLang的GOROOT(2) 可以不配置GoLang的GOPATH(3) 配置mod模块的状态

export GO111MODULE=auto
可选值有三个:on:开启,off:关闭,auto:自动
如果是自动模式,文件夹下有go.mod则是开启状态,否则是关闭状态
如果是开启状态,则GOPATH失效

(4) 配置mod的国内代理

#这是阿里的配置
export GOPROXY=https://mirrors.aliyun.com/goproxy/

在goland中的使用

打开goland的设置,然后配置如下:

gomod.jpg![file](https://img-blog.csdnimg.cn/20191025140904293.jpeg?x-oss-process=image/watermark,typeZmFuZ3poZW5naGVpdGk,shadow10,textaHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1BuZnlfX1NoYW4=,size16,colorFFFFFF,t_70)

使用

初始化modules

$ go mod init bitcoin   
go: creating new go.mod: module bitcoin

代码编辑

可以引入 GoLang 的官方包,也可以引入非官方包

导入代码需要的依赖

$ go mod tidy 

常用命令

$ go help mod    
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.

翻译一下

Go mod提供对模块操作的访问。

注意,所有的go命令都内置了对模块的支持,而不仅仅是"go mod"。例如,日常应该使用"go get"来完成依赖的添加、删除、升级、降级。
更多模块功能的概述,请参阅"go help modules",我把它放在附录里了。

用法:
    go mod <命令> [参数]
命令如下:
    download    将模块下载到本地缓存
    edit        通过工具或脚本编辑"go.mod"
    graph        打印模块需求图
    init        当前目录下初始化新的模块,后接参数模块名
    tidy        添加需要的依赖和删除未使用的依赖
    verdor        把依赖项复制到vendor文件中
    verify        验证依赖项是否被修改过
    why        解释为什么需要包或模块

常见的问题

问题一

$ go mod init
go: cannot determine module path for source directory /Users/shanpengfei/work/go-work-space/src/github.com/shanpengfei7/bitcoin (outside GOPATH, no import comments)

解决方法:init后面应该加上模块的名字

$ go mod init bitcoin   
go: creating new go.mod: module bitcoin

问题二

$ go mod init
go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'

$ go mod init bitcoin
go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'

解决方法:不要在$GOPATH路径下执行该命令。或者换个路径,或者把环境变量中的$GOPATH置空

附录

看一下官方介绍 go module 的原文:

A module is a collection of related Go packages.
Modules are the unit of source code interchange and versioning.
The go command has direct support for working with modules,
including recording and resolving dependencies on other modules.
Modules replace the old GOPATH-based approach to specifying
which source files are used in a given build.

Preliminary module support

Go 1.11 includes preliminary support for Go modules,
including a new module-aware 'go get' command.
We intend to keep revising this support, while preserving compatibility,
until it can be declared official (no longer preliminary),
and then at a later point we may remove support for work
in GOPATH and the old 'go get' command.

The quickest way to take advantage of the new Go 1.11 module support
is to check out your repository into a directory outside GOPATH/src,
create a go.mod file (described in the next section) there, and run
go commands from within that file tre
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值