草庐IT

golang 在子目录中找不到包 gin

coder 2024-07-11 原文

我收到以下错误消息:

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

go 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结构


我确实删除了。 并再次安装 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 示例代码运行正常。 当我用 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)

(这次没有下划线)

不走运,重新安装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$

最佳答案

如果在项目的根路径中没有启用 go mod,那么你必须启用 go mod 并安装包。

例如,缺少名为“/gin-gonic/gin”的包,或者您找不到它。

请在 src 文件夹或与 github.com 文件夹并行运行。

$ GO111MODULE=on go get -u github.com/gin-gonic/gin@v1.3.0
  1. 我假设您的 $GOPATH 是 Users/YourUserName/go

希望对您有所帮助。

关于golang 在子目录中找不到包 gin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51488385/

有关golang 在子目录中找不到包 gin的更多相关文章

  1. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  2. ruby-on-rails - 如何在 Gem 中获取 Rails 应用程序的根目录 - 2

    是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在

  3. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  4. ruby-on-rails - 没有这样的文件或目录 - 用 Mini Magick 识别 - 2

    在我让另一个人重做我的前端UI之前,我的Rails应用程序运行平稳。我已经尝试解决此错误3天了。这是错误:Nosuchfileordirectory-identifyExtractedsource(aroundline#59):575859606162@post=Post.find(params[:id])authorize@postif@post.update_attributes(post_params)flash[:notice]="Postwasupdated."redirect_to[@topic,@post]else{"utf8"=>"✓","_method"=>"patc

  5. ruby-on-rails - 找不到 gem railties (>= 0.a) (Gem::GemNotFoundException) - 2

    我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby​​2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10

  6. 即使安装了 gem,Ruby 也找不到所需的库 - 2

    我花了几天时间尝试安装ruby​​1.9.2并让它与gems一起工作:-/我最终放弃了我的MacOSX10.6机器,下面是我的Ubuntu机器上的当前状态。任何建议将不胜感激!#rubytest.rb:29:in`require':nosuchfiletoload--mongo(LoadError)from:29:in`require'fromtest.rb:1:in`'#cattest.rbrequire'mongo'db=Mongo::Connection.new.db("mydb")#gemwhichmongo/usr/local/rvm/gems/ruby-1.9.2-p0/g

  7. ruby - Sinatra 找不到 View 目录 - 2

    我正在尝试以一种更类似于普通RubyGem结构的方式构建我的Sinatra应用程序。我有以下文件树:.├──app.rb├──config.ru├──Gemfile├──Gemfile.lock├──helpers│  ├──dbconfig.rb│  ├──functions.rb│  └──init.rb├──hidden│  └──Rakefile├──lib│  ├──admin.rb│  ├──api.rb│  ├──indexer.rb│  ├──init.rb│  └──magnet.rb├──models│  ├──init.rb│  ├──invite.rb│  ├─

  8. ruby - 在 SUSE 上找不到 Ruby 的头文件? - 2

    我正在尝试在SUSEEnterprise11SP3上安装compass。我得到以下信息。有什么想法吗?geminstallcompassBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingcompass:ERROR:Failedtobuildgemnativeextension./usr/bin/rubyextconf.rbmkmf.rbcan'tfindheaderfilesforrubyat/usr/lib64/ruby/ruby.hextconffailed,exitcode1Gemfileswi

  9. ruby - 如何在 ruby​​ 中复制目录结构,不包括某些文件扩展名 - 2

    我想编写一个ruby​​脚本来递归复制目录结构,但排除某些文件类型。因此,给定以下目录结构:folder1folder2file1.txtfile2.txtfile3.csfile4.htmlfolder2folder3file4.dll我想复制这个结构,但不包含.txt和.cs文件。因此,生成的目录结构应如下所示:folder1folder2file4.htmlfolder2folder3file4.dll 最佳答案 您可以使用查找模块。这是一个代码片段:require"find"ignored_extensions=[".cs"

  10. ruby-on-rails - Heroku 找不到 SecureRandom - 2

    我的heroku应用崩溃了,因为它找不到模块“SecureRandom”。我在gemfile中指定了我的Ruby版本,我的计算机、gemfile和Heroku似乎都匹配Ruby版本号,尽管不是补丁号。其他帖子建议将usr/bin/heroku指向特定的Ruby文件,但我不确定该怎么做(我的应用程序中没有Herokubin)。这看起来像是Ruby版本错误。我该如何解决这个问题?classOrderItemHeroku日志:/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.3/lib/active_support/dependenci

随机推荐