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.
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 .
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".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.