• 2.9 go mod 之本地仓库搭建


    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

  • 相关阅读:
    大战设计模式【13】—— 组合模式
    大战设计模式【12】—— 迭代器模式
    大战设计模式【11】—— 模板方法模式
    大战设计模式【10】—— 外观模式
    linux命令进阶
    ansible普通用户su切换
    Ansible 进阶技巧
    ansible playbook对错误的处理
    ansible示例,离线安装etcd
    (原)centos7安装和使用greenplum4.3.12(详细版)
  • 原文地址:https://www.cnblogs.com/perfei/p/11398090.html
Copyright © 2020-2023  润新知