我得到以下错误消息:
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" ) : errorgo env看起来没问题。
更多代码:
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结构
我确实删除了go。并再次安装flow 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示例代码,运行ok。当我用user.go添加控制器文件夹时
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"添加到main函数
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)(这次不加下划线)
运气不佳,请重新安装go
我能找到路径,但找不到下划线的东西
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$发布于 2019-10-10 11:54:12
如果在项目的根路径中没有启用go mod,那么您必须启用go mod并安装包。
例如,一个名为"/gin-gonic/gin“的包丢失了,或者你找不到它。
请在src文件夹或平行于github.com文件夹中运行此程序。
$ GO111MODULE=on go get -u github.com/gin-gonic/gin@v1.3.0我假设你的$GOPATH是Users/YourUserName/go
我希望这能帮到你。
发布于 2021-03-18 21:08:14
从Go v1.11开始,你可以使用go模块,它允许你在Go src/文件夹之外创建带有模块的项目。为此,您只需在根文件夹中运行命令go build即可。
本文将对此进行全面解释:https://medium.com/mindorks/create-projects-independent-of-gopath-using-go-modules-802260cdfb51
Gl和Hf :D
发布于 2018-07-24 17:44:27
也许我只是忽略了项目文件夹中每个文件夹的这一步。(非常重要)
mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_" 我一直在问我的朋友谷歌,答案是: goVendor非常依赖$GOPHATH。然后我用上面的命令创建了控制器文件夹。所以每个文件夹也是如此。没有问题:)
感谢所有帮助和查看的人
https://stackoverflow.com/questions/51488385
复制相似问题