golang 1.4 mod 使用经验

2020-02-29  本文已影响0人  perryn

1.go mod 替代原来gopath的功能,依赖包下载依赖Go环境变量

export GO111MODULE=on
export GOPROXY=https://mirrors.aliyun.com/goproxy/

2.go mod init 一个golang 项目

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


//进入glusterfs-benchmark目录
cd glusterfs-benchmark 
go mod init glusterfs-benchmark 
//会在 lusterfs-benchmark目录中  生成go.mod的文件
[perrynzhou@Debian ~/Source/perryn/glusterfs-benchmark]$ tree ../glusterfs-benchmark/
../glusterfs-benchmark/
├── api
│   ├── fuse_fetch.go
│   └── glfs_fetch.go
├── conf
│   └── conf.go
├── go.mod
├── metric
│   └── metric.go
├── pkg
│   └── mod
│       └── cache
│           └── lock
└── utils
    └── utils.go

14 directories, 114 files

// fuse_fetch.go的包信息
package api

import (
    "bufio"
    "fmt"
    log "github.com/sirupsen/logrus"
    "glusterfs-benchmark/conf"
    "glusterfs-benchmark/metric"
    "glusterfs-benchmark/utils"
    "os"
    "path/filepath"
    "sync"
    "sync/atomic"
)
//修改go.mod文件
module glusterfs-benchmark

go 1.14

require github.com/sirupsen/logrus v1.4.2

//进入包含main.go的目录下执行go mod vendor,保存依赖包
go mod vendor
//下载第三方依赖库
go mod tidy -v
//编译项目
go build -mod=vendor
上一篇 下一篇

猜你喜欢

热点阅读