Golang-为什么我不能在GOPATH/src/project中导入本地软件包,而不能在主目录中导入本地软件包?
我有一个项目,其文件夹结构如下:
I have a project, its folder structure is like following:
/project
models/
Product.go
main.go
main.go的内容是:
The content of main.go is:
package main
import (
"./models"
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
fmt.Println(models.Product{})
r.GET("/", func(c *gin.Context) {
c.String(200, "he")
})
r.Run(":3000")
}
Product.go的内容为:
The content of Product.go is:
package models
type Product struct {
Name string
}
我从键入go env
得到的是:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/Mac/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.5.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.5.3/libexec/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT=""
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments - fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
当项目目录的位置为$GOPATH/src/project
时,如果运行go run main.go
,我得到的是以下错误消息:./main.go:: can't find import: "github.com/gin-gonic/gin"
.
When the location of the project directory is $GOPATH/src/project
, If I run go run main.go
, what I get is this error message: ./main.go:: can't find import: "github.com/gin-gonic/gin"
.
但是当项目目录的位置为~/project
时,go run main.go
可以正常工作.
But when the location of project directory is ~/project
, go run main.go
can work as expected.
我使用go1.5.3.
I use go1.5.3.
任何人都可以帮助我.谢谢.
Can anyone help me. Thanks.
相对导入路径仅是为了方便起见,主要用于实验. go build
和go install
不完全支持它们.如果您想让包与go
工具一起使用,请不要使用相对导入.按照如何编写Go代码中所述构造代码.
Relative import paths are only allowed as a convenience, mostly for experimentation. They are not fully supported by go build
and go install
. If you want your package to work with the go
tools, don't use relative imports. Structure your code as described in How to Write Go Code.