查看go mod命令:

PS C:\Users\zhaoh\go> go mod help
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

Use "go help mod <command>" for more information about a command.
download      将模块下载到本地缓存
edit          从工具或者脚本编辑go.mod
graph         打印模块需求图
init          初始化当前目录中的新模块
tidy          添加丢失的和删除未使用的模块
vendor        创建依赖项的vendored副本
verify        验证依赖项是否具有预期的内容
why           解释为什么需要包或模块

在非GOPATH目录下初始化当前新模块,会多出来一个go.mod文件,记录的是依赖管理。

PS C:\Users\zhaoh\go> cd ../
PS C:\Users\zhaoh> mkdir hello


    目录: C:\Users\zhaoh


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2019/10/18     17:21                hello


PS C:\Users\zhaoh> cd hello
PS C:\Users\zhaoh\hello> go mod init hello
go: creating new go.mod: module hello
PS C:\Users\zhaoh\hello> ls


    目录: C:\Users\zhaoh\hello


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2019/10/18     17:25             13 go.mod


PS C:\Users\zhaoh\hello> cat go.mod
module hello

在hello下新建一个main.go文件。

package main

import (
    "fmt"
)

func main() {
    fmt.Println("hello world")
}

在终端执行go run命令:

PS C:\Users\zhaoh\hello> go run main.go
hello world
PS C:\Users\zhaoh\hello> cat go.mod
module hello

require github.com/labstack/echo v3.3.10+incompatible // indirect
PS C:\Users\zhaoh\hello> cat go.sum
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
PS C:\Users\zhaoh\hello>

可以发现go.mod文件已经发生变化,并且多出来一个go.sum文件,用来记录版本变更。
要注意一下虽然是在非GOPATH路径下执行go mod命令,但go mod下载的依赖包依旧储存在GOPATH的pkg下:
!1571399347672-46980be0-2424-431b-8cf0-8dc1c9f404db.png

Last modification:October 19th, 2019 at 05:34 pm
如果觉得我的文章对你有用,请随意赞赏