[踩坑] 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 协议》,转载必须注明作者和本文链接