[英]golang can not find package gin in sub directory
I get the following error message:我收到以下错误消息:
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)
go env去环境
GOPATH="/home/ubuntu/goapi"
GOROOT="/usr/local/go"
folder structure文件夹结构
github.com/rose
api
main.go // the loading gin is ok here
controller
|-user.go //with import ( "github.com/gin-gonic/gin" ) : error
go env looks Ok. go env 看起来不错。
Some more code:还有一些代码:
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) {
example resource: https://github.com/thearavind/go-gin-pg , nothing wrong with this example.示例资源: https : //github.com/thearavind/go-gin-pg ,这个示例没有任何问题。 I just make it like MVC structure我只是让它像 MVC 结构
I did remove go.我确实删除了去。 and install it again flow Linux version https://golang.org/doc/install then并再次安装它流 Linux 版本https://golang.org/doc/install然后
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
then https://github.com/gin-gonic/gin#use-a-vendor-tool-like-govendor the example code run ok.然后https://github.com/gin-gonic/gin#use-a-vendor-tool-like-govendor示例代码运行正常。 when I add controller folder with user.go with当我用 user.go 添加控制器文件夹时
import( "github.com/gin-gonic/gin")
full code完整代码
package controller
import( "github.com/gin-gonic/gin")
func somestring(){
return "hello world"
}
in the main.go i use above在 main.go 我在上面使用
curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go
add to import添加到导入
"./controller"
"fmt"
add to the main function添加到主函数
user := new(controller.somestring)
fmt.Printf(user)
I understand this is not a good code for me, but will produce this error again, like so:我知道这对我来说不是一个好的代码,但会再次产生此错误,如下所示:
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)
(no underscore this time) (这次没有下划线)
No luck, reinstall go没有运气,重新安装去
I am able to find the path, but not the underscore thing我能找到路径,但不能找到下划线
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$
If go mod is not enabled in root path of project then you have to enable go mod and install the package.如果在项目的根路径中没有启用 go mod,那么你必须启用 go mod 并安装包。
For example , a package named "/gin-gonic/gin" is missing or you cannot find this.例如,缺少一个名为“/gin-gonic/gin”的包,或者您找不到它。
Please run this in src folder or parallel to github.com folder.请在 src 文件夹或与 github.com 文件夹并行运行它。
$ GO111MODULE=on go get -u github.com/gin-gonic/gin@v1.3.0
I hope this helps.我希望这有帮助。
since Go v1.11, you can use go modules which let you create project with modules outside of the Go src/ folder.从 Go v1.11 开始,您可以使用 go modules 来创建带有 Go src/ 文件夹之外的模块的项目。 To do so, you can simply run the command go build
at your root folder.为此,您只需在根文件夹中运行go build
命令go build
。
This article explains it all: https://medium.com/mindorks/create-projects-independent-of-gopath-using-go-modules-802260cdfb51这篇文章解释了这一切: https : //medium.com/mindorks/create-projects-independent-of-gopath-using-go-modules-802260cdfb51
Gl & Hf :D Gl & Hf :D
maybe I just miss out this step for every folder in the project folder.也许我只是错过了项目文件夹中每个文件夹的这一步。 (very important) (很重要)
mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
I keep asking my friend Google, the answer is: goVendor depend on the $GOPHATH a lot.我一直问我的朋友谷歌,答案是:goVendor 非常依赖于 $GOPHATH。 then I Create the controller folder with above command.然后我用上面的命令创建控制器文件夹。 so every folder too.所以每个文件夹也是。 no problem :)没问题 :)
Thank you to everyone Helping and Viewing感谢大家的帮助和观看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.