为什么我无法在GOPATH/src/project中导入本地包,但可以在主目录中导入?

huangapple go评论74阅读模式
英文:

Golang - Why can't I import local package in GOPATH/src/project but can in home directory?

问题

我有一个项目,它的文件夹结构如下:

    /project
        models/
            Product.go
        main.go

main.go的内容如下:

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的内容如下:

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"

但是当项目目录的位置是~/project时,go run main.go可以正常工作。

我使用的是go1.5.3版本。

有人可以帮助我吗?谢谢。

英文:

I have a project, its folder structure is like following:

    /project
        models/
            Product.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")
}

The content of Product.go is:

package models

type Product struct {
    Name string
}

What I get from typing go env is:

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"

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".

But when the location of project directory is ~/project, go run main.go can work as expected.

I use go1.5.3.

Can anyone help me. Thanks.

答案1

得分: 5

相对导入路径仅作为一种方便的方式存在,主要用于实验目的。它们并不被go buildgo 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.

答案2

得分: -1

你的代码中使用了"github.com/gin-gonic/gin",这是一个外部导入的包。因此,Go编译器会尝试在你的工作区中找到这个包。所以你需要手动在Go工作区或构建路径中下载这些包,使用命令"go get github.com/gin-gonic/gin"。

英文:

As you are using "github.com/gin-gonic/gin" in your code. It is an external import. So go compiler will try to find this package in your workspace. So you need manually download those package in your go workspace or build path
"go get github.com/gin-gonic/gin".

huangapple
  • 本文由 发表于 2016年1月16日 00:21:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/34815455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定