[踩坑] Go Modules 使用
go 版本#
go version
go version go1.13.4 darwin/amd64
初始化项目#
1,通常使用 go mod init 项目目录。如下:
go mod init go-project
cat go.mod
//
module go-project
go 1.13
require (
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 // indirect
)
2. 在目录 go-project 下有以下目录及文件
test 目录和 main.go 文件,test 目录有 test.go 文件,test.go 文件中有 Hello 方法。那么在 main.go 文件中使用 test 目录下的 test.go 文件中的 Hello 方法
main.go
package main
import (
"go-project/test/test"
"fmt"
)
func main() {
t : = test.Hello()
fmt.Println(t)
}
执行 go run main.go. 报错
build command-line-arguments: cannot load go-project/test/test: malformed module path "go-project/test/test": missing dot in first path element
解决方法#
把 go.mod 中 module go-project 改为 test.com/go-project
cat go.mod
//
module test.com/go-project
go 1.13
require (
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 // indirect
)
main.go 文件中引入如下
main.go
package main
import (
"test.com/go-project/test/test"
"fmt"
)
func main() {
t : = test.Hello()
fmt.Println(t)
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: