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. 【.NET 与树莓派】用 MPD 制作数字音乐播放器

    树莓派的日常家居玩法多多,制作一台属于自己的数字音乐播放机是其中的一种.严格上说,树莓派是没有声卡的,其板载的 3.5 mm 音频孔实际是通过 PWM 来实现音频输出的(通过算法让PWM信号变成模拟信 ...

  2. 讲分布式唯一id,这篇文章很实在

    分布式唯一ID介绍 分布式系统全局唯一的 id 是所有系统都会遇到的场景,往往会被用在搜索,存储方面,用于作为唯一的标识或者排序,比如全局唯一的订单号,优惠券的券码等,如果出现两个相同的订单号,对于用 ...

  3. Bootstrap-2栅格系统

    栅格系统(使用最新版本bootstrap) Grid options(网格配置) Responsive classes(响应式class) Gutters(间距) Alignment(对齐方式) Re ...

  4. 查看Git提交的代码统计

    1,提交Top5: git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5 2,某用户提交的代码统计 git log ...

  5. 第一周PTA笔记 德州扑克题解

    德州扑克 最近,阿夸迷于德州扑克.所以她找到了很多人和她一起玩.由于人数众多,阿夸必须更改游戏规则: 所有扑克牌均只看数字,不计花色. 每张卡的值为1.2.3.4.5.6.7.8.9.10.11.12 ...

  6. IntelliJ IDEA竟然出了可以在云端编码的功能?

    前言 自从我用了正版的IntelliJ IDEA后,基本上都是与时俱进,出一个新版本就立马更新,这也能能让我体验到最新最快的功能. 最近在闲逛Jetbrains的官网时,看到了最新的2021.3EAP ...

  7. Python 函数 参数传递

    参数传递    在 python 中,类型属于对象,变量是没有类型的:        a=[1,2,3]        a="Runoob"    以上代码中,[1,2,3] 是 ...

  8. PHP高级特性-反射Reflection以及Factory工厂设计模式的结合使用[代码实例]

    PHP高级特性-反射以及工厂设计模式的结合使用 [结合 Laravel-Admin 代码实例讲解] 利用反射来实现工厂模式的生产而无需创建特定的工厂类 本文地址http://janrs.com/?p= ...

  9. Chrome 插件特性及实战场景案例分析

    一.前言 提起Chrome扩展插件(Chrome Extension),每个人的浏览器中或多或少都安装了几个插件,像一键翻译.广告屏蔽.录屏等等,通过使用这些插件,可以有效的提高我们的工作效率:但有时 ...

  10. 反调试代码调试死机代码禁止F12代码

    反调试代码调试死机代码禁止F12代码  // 反调试函数,参数:开关,执行代码 function siji(){    var total="";    for (var i=0; ...