wiki
https://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release
参考
https://blog.csdn.net/benben_2015/article/details/82227338

go mod 之本地仓库搭建
----------------------------------------------------------------------------------------
将当前项目添加到$GOPATH中--这是使用的前提条件
11版本中的临时变量on/off,on表示使用go mod同时禁用$GOPATH
export GO111MODULE=on
export GO111MODULE=off

在各个包下面执行
go mod init

在main方法所在的包下执行
go mod init

修改main程序包下的go.mod,将需要的包添加进来
vim go.mod
module test

require mha v0.0.0
replace mha => ../mha

go 1.12

go.mod说明
--------------------------------------------------
module test中的test是指GOPATH src下的全路径,这里就是$GOPATH/src/test
require mha中的mha也是如此
如果在github上的话,这里的路径将是 github.com/项目名称/包名称
replace 则是使用本地仓库的包,替换指定的包
如果使用了go mod,那么包的引入规则只能全部以该方式进行,不能部分包使用go mod,部分包还使用$GOPATH
export GO111MODULE=off 禁用 go mod后,$GOPATH生效,不需要删除各包下的go.mod文件,原来的项目依然可以运行

再看一个更详细的例子

===========================================================================

GOPATH目录为空
root@black:~# echo $GOPATH
/opt/code/gopath
root@black:~# cd /opt/code/gopath/
root@black:/opt/code/gopath# ls
bin src
root@black:/opt/code/gopath# cd src
root@black:/opt/code/gopath/src# ls
root@black:/opt/code/gopath/src#

mkdir test
vim test/test.go
package main

import(
"fmt"
"time"
)

func test(){
c := make(chan struct{})

go func(){
fmt.Println("我要出去看看园子里的花还活着吗")
time.Sleep(7*time.Second)
c <- struct{}{}
}()

<- c
fmt.Println("这花被别人拿走了,再也看不到它了")
}

func main(){
test()
}

# go run test/test.go
我要出去看看园子里的花还活着吗
这花被别人拿走了,再也看不到它了

上面是GOPATH方式运行的程序,现在以go mod方式运行

打开MOD打开
export GO111MODULE=on

初始化
cd test
go mod init
go: cannot determine module path for source directory /opt/dev/test (outside GOPATH, no import comments)
这里说我们的项目根目录必须位于GOPATH中,那么,我们将项目根目录加入到GOPATH中

export GOPATH=/opt/code/gopath
export GOPATH=$GOPATH:/opt/dev/test
source /etc/profile
# echo $GOPATH
/opt/code/gopath:/opt/dev/test

cd /opt/dev/test
mkdir src
将之前的脚本目录移动过来

再次运行go mod init
root@black:/opt/dev/test/src/test# go mod init
go: creating new go.mod: module test
root@black:/opt/dev/test/src/test# cat go.mod
module test

go 1.12

root@black:/opt/dev/test/src/test# go run test.go
我要出去看看园子里的花还活着吗
这花被别人拿走了,再也看不到它了

再看一个稍微复杂的例子,以go1.13为例

=======================================

项目src目录有以下目录

common

config

model

tools

main/esql

共五个包,每个包下面都需要执行go mod init

tools中引用了common、config、model包

main/esql引用了tools

本地mod, 版本号的数字,可以随意写

common go.mod

module common

go 1.13

config go.mod

module config

go 1.13

model go.mod

module model

go 1.13

tools go.mod

module tools

go 1.13

require (
common v0.0.0
config v0.0.9
model v0.0.0
) replace common => ../common replace config => ../config replace model => ../model

main/esql go.mod

module main/esql

go 1.13

require (
common v0.0.0
config v0.0.9
model v0.0.0
tools v0.0.0
) replace tools => ../../tools replace common => ../../common replace config => ../../config replace model => ../../model

其他go mod相关命令

go clean -modcache

go mod自动下载路径

----------------------------------------------

# echo $GOPATH
/opt/wks/gopath:/opt/wks/wks_go/9db

# ll /opt/wks/gopath/pkg/mod/cache/download/github.com/baidu/go-lib
total 12
drwxr-xr-x 3 tan tan 4096 11月 28 22:44 ./
drwxr-xr-x 3 tan tan 4096 11月 28 22:44 ../
drwxr-xr-x 2 tan tan 4096 2月 20 14:27 '@v'/

下载编译他人的gom项目,以gotop项目为例

---------------------------------------------------------------------

go get github.com/cjbassi/gotop

tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ echo $GOPATH
/opt/wks/gopath:/opt/wks/wks_go/9db:/opt/wks/wks_go/dbm
tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ ll

tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ go build -o gotop main.go
go: downloading github.com/gizak/termui/v3 v3.0.0
go: downloading github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815
go: downloading github.com/shirou/gopsutil v2.18.11+incompatible
go: downloading github.com/distatus/battery v0.9.0
go: extracting github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815
go: extracting github.com/distatus/battery v0.9.0
go: extracting github.com/gizak/termui/v3 v3.0.0
go: downloading github.com/mitchellh/go-wordwrap v1.0.0
go: downloading github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
go: downloading github.com/mattn/go-runewidth v0.0.4
go: downloading github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd
go: extracting github.com/shirou/gopsutil v2.18.11+incompatible
go: extracting github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd
go: extracting github.com/mitchellh/go-wordwrap v1.0.0
go: extracting github.com/mattn/go-runewidth v0.0.4
go: extracting github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
go: downloading golang.org/x/sys v0.0.0-20190116161447-11f53e031339
go: extracting golang.org/x/sys v0.0.0-20190116161447-11f53e031339
go: finding github.com/gizak/termui/v3 v3.0.0
go: finding github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd
go: finding github.com/mattn/go-runewidth v0.0.4
go: finding github.com/mitchellh/go-wordwrap v1.0.0
go: finding github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
go: finding github.com/distatus/battery v0.9.0
go: finding github.com/shirou/gopsutil v2.18.11+incompatible
go: finding golang.org/x/sys v0.0.0-20190116161447-11f53e031339
go: finding github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815
tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ ll
total 4360
drwxr-xr-x 12 tan tan 4096 4月 4 09:57 ./
drwxr-xr-x 3 tan tan 4096 4月 4 09:52 ../
drwxr-xr-x 5 tan tan 4096 4月 4 09:52 assets/
drwxr-xr-x 2 tan tan 4096 4月 4 09:52 build/
-rw-r--r-- 1 tan tan 2532 4月 4 09:52 CHANGELOG.md
drwxr-xr-x 2 tan tan 4096 4月 4 09:52 ci/
drwxr-xr-x 2 tan tan 4096 4月 4 09:52 colorschemes/
drwxr-xr-x 8 tan tan 4096 4月 4 09:52 .git/
drwxr-xr-x 3 tan tan 4096 4月 4 09:52 .github/
-rw-r--r-- 1 tan tan 108 4月 4 09:52 .gitignore
-rw-r--r-- 1 tan tan 809 4月 4 09:57 go.mod
-rw-r--r-- 1 tan tan 4399 4月 4 09:52 go.sum
-rwxr-xr-x 1 tan tan 4331298 4月 4 09:57 gotop*
-rw-r--r-- 1 tan tan 34520 4月 4 09:52 LICENSE
-rw-r--r-- 1 tan tan 10825 4月 4 09:52 main.go
-rw-r--r-- 1 tan tan 797 4月 4 09:52 Makefile
-rw-r--r-- 1 tan tan 3904 4月 4 09:52 README.md
drwxr-xr-x 2 tan tan 4096 4月 4 09:52 scripts/
drwxr-xr-x 2 tan tan 4096 4月 4 09:52 snap/
drwxr-xr-x 6 tan tan 4096 4月 4 09:52 src/
-rw-r--r-- 1 tan tan 747 4月 4 09:52 .travis.yml
drwxr-xr-x 5 tan tan 4096 4月 4 09:52 vendor/
tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ ./gotop

2.9 go mod 之本地仓库搭建的更多相关文章

  1. 3.0 go mod之远程仓库搭建-代码示例

    注意事项 所谓的远程仓库指的是github,个人首次使用go mod在其他云仓库上尝试,并未成功,这浪费了我近2小时的时间: 如果你是初次尝试,那么除了github的地址换一下之外,其他的都按照示例操 ...

  2. yum 本地仓库搭建

    一,配置yum源 设置镜像 挂载 查看是否挂在成功 复制镜像内容到opt下面 删除不相关内容 进入/mnt/Packages 安装生成缓存文件 选择这个结尾的 更新本地yum源 yum clean a ...

  3. heartbeat 非联网安装(通过配置本地yum文件库安装heartbeat)

    软件环境:centos6.5 一.下载rpm包 首先找一台联网的centos6.5机器 安装epel扩展源: yum install -y epel-release 安装yum-plugin-down ...

  4. Maven的安装与本地仓库的搭建

    Maven的安装 1.首先去官网下载maven.http://maven.apache.org/download.cgi 2.解压下载后的压缩包.例如到D盘.D:\apache-maven-3.5.0 ...

  5. 如何在VMware下通过挂载系统光盘搭建本地yum软件仓库

    1.打开自己VMware软件中的虚拟机 2.输入用户名root密码登陆进去 3.打开 应用程序----收藏-----终端 4.首先回到根目录,输入cd ..(中间有空格) 5.创建一个名称为swlaa ...

  6. 通过挂载系统U盘搭建本地yum仓库

    首先打开hbza(CentOS)和yum,两者要连接上 第1步:在hbza中创建一个目录 输入mkdir /lxk,名字随便起.输入mount  /dev/cdrom  /lxk 第2步:打开yum, ...

  7. 【JavaScript】停不下来的前端,自动化流程

    http://kb.cnblogs.com/page/501270/ 流程 关于流程,是从项目启动到发布的过程.在前端通常我们都做些什么? 切图,即从设计稿中获取需要的素材,并不是所有前端开发都被要求 ...

  8. golang (5) ---工程管理

    1. go mod 添加本地package依赖 go mod 作为golang新的版本管理工具,减少了对GOPATH的依赖. 但是对本地文件的依赖的时候,提示 build server: cannot ...

  9. Linux软件包管理和磁盘管理实践

    一.自建yum仓库,分别为网络源和本地源 本地yum仓库的搭建就是以下三个步骤: 创建仓库目录结构 上传相应的包到目录下,或者直接挂载光盘也行,如果挂载光盘,第三步就可以省略,因为光盘默认里有repo ...

随机推荐

  1. Sentinel-Go 源码系列(二)|初始化流程和责任链设计模式

    上节中我们知道了 Sentinel-Go 大概能做什么事情,最简单的例子如何跑起来 其实我早就写好了本系列的第二篇,但迟迟没有发布,感觉光初始化流程显得有些单一,于是又补充了责任链模式,二合一,内容显 ...

  2. JMeter学习笔记--JDBC测试计划-连接Mysql

    1.首先要下载jar包,mysql-connector-java-5.1.7-bin.jar 放到Jmeter的lib文件下ext下 2.添加JDBC Connection Configuration ...

  3. MyScript 开发文档

    一.IInk SDK runtime 1.1 引擎创建 1.2 对象释放 1.3 获取并设置配置 配置 访问配置 配置识别 二.文件存储 2.1 支持的内容的类型 2.2 模型结构 2.3 Conte ...

  4. Python | Python语法基础

    目录 前言 1. 变量与简单数据结构 2. 列表相关 3. 集合 4. If语句 5. 字典 6. 用户输入和while循环 7. 函数 8. 类与对象 9. 文件 10. 异常 11. 测试 最后 ...

  5. [atARC110E]Shorten ABC

    考虑令$a$.$b$和$c$分别对应1.2和3,那么每一次相当于令$x$和$y$变为$x\oplus y$(要求$x\ne y$) 根据异或的结合律,我们相当于将其划分为若干个区间求异或值 (另外还有 ...

  6. Sentry 监控 - Snuba 数据中台架构(SnQL 查询语言简介)

    本文描述了 Snuba 查询语言 (SnQL). 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒 ...

  7. Educational Codeforces Round 89 题解

    昨晚简单 vp 了场比赛找了找状态,切了 5 个题(有一个差点调出来),rk57,还算理想吧,毕竟我已经好久没碰过电脑了( A 签到题不多说,直接输出 \(\min\{a,b,\dfrac{a+b}{ ...

  8. RSA,DSA,ECDSA,EdDSA和Ed25519的区别

    RSA,DSA,ECDSA,EdDSA和Ed25519的区别 用过ssh的朋友都知道,ssh key的类型有很多种,比如dsa.rsa. ecdsa.ed25519等,那这么多种类型,我们要如何选择呢 ...

  9. Generic recipe for data analysis with general linear model

    Generic recipe for data analysis with general linear model Courtesy of David Schneider State populat ...

  10. linux下面升级 Python版本并修改yum属性信息

    最近需要在linux下使用python,故需要升级一下python版本,上网查询了一下相关资料,更新了一下linux下面的python环境,记录如下: linux下面升级 Python版本并修改yum ...