golang 1.4 mod 使用经验

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

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

2.go mod init 一个golang 项目

  • go mod 的命令使用
$ go help mod
Usage:

        go mod  [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


  • 用go mod 初始化glusterfs-benchmark依赖包管理
//进入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

  • 引入glusterfs-benchmark/api/fuse_fetch.go中引入utils库本地库
// 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

你可能感兴趣的:(golang 1.4 mod 使用经验)