我收到以下错误消息:
controllers/user.go:4:2: cannot find package "(underscore)/home/ubuntu/goapi/src/github.com/roes/api/vendor/github.com/gin-gonic/gin"
in any of: /usr/local/go/src/(underscore)/home/ubuntu/goapi/src/github.com/roes/api/vendor/github.com/gin-gonic/gin (from $GOROOT)
/home/ubuntu/goapi/src/_(underscore)/home/ubuntu/goapi/src/github.com/roes/api/vendor/github.com/gin-gonic/gin (from $GOPATH)
去环境GOPATH="/home/ubuntu/goapi"
GOROOT="/usr/local/go"
文件夹结构github.com/rose
api
main.go // the loading gin is ok here
controller
|-user.go //with import ( "github.com/gin-gonic/gin" ) : error
去环境看起来还可以。一些代码:
package controller
import (
"github.com/gin-gonic/gin"
"net/http"
// "github.com/astaxie/beego/orm"
"../database"
"../models"
)
func init() {
database.ConnectToDb()
ORM = database.GetOrmObject()
}
//UserController ...
type UserController struct{}
func createUser(c *gin.Context) {
示例资源:https://github.com/thearavind/go-gin-pg,此示例没有错。我只是像MVC结构一样我确实删除了。
并再次安装流Linux版本
https://golang.org/doc/install
然后
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
然后https://github.com/gin-gonic/gin#use-a-vendor-tool-like-govendor
示例代码运行正常。
当我用user.go添加 Controller 文件夹时
import( "github.com/gin-gonic/gin")
完整的代码 package controller
import( "github.com/gin-gonic/gin")
func somestring(){
return "hello world"
}
在main.go中,我在上面使用 curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go
添加到导入 "./controller"
"fmt"
添加到主要功能 user := new(controller.somestring)
fmt.Printf(user)
我了解这对我来说不是一个好代码,但是会再次产生此错误,如下所示: controller/user.go:4:2: cannot find package "github.com/gin-gonic/gin" in any of:
/usr/local/go/src/github.com/gin-gonic/gin (from $GOROOT)
/home/ubuntu/go/src/github.com/gin-gonic/gin (from $GOPATH)
(这次没有下划线)没运气,重新安装去
我可以找到路径,但不能找到下划线
08:56:35 ~/go/src/github.com/jerry/core$ cd /home/ubuntu/go/src/github.com/jerry/core/vendor/github.com/gin-gonic/
08:56:45 ~/go/src/github.com/jerry/core/vendor/github.com/gin-gonic$
最佳答案
如果未在项目的根路径中启用go mod,则必须启用go mod并安装软件包。
例如,缺少一个名为“/gin-gonic/gin”的软件包,或者您找不到该软件包。
请在src文件夹中或与github.com文件夹并行运行。
$ GO111MODULE=on go get -u github.com/gin-gonic/[email protected]
我希望这有帮助。
关于go - golang在子目录中找不到软件包gin,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51488385/