【问题标题】:golang can not find package gin in sub directorygolang 在子目录中找不到包 gin
【发布时间】:2018-12-31 11:19:31
【问题描述】:

我收到以下错误消息:

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


我确实删除了 go。 并再次安装流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 添加控制器文件夹时

 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$

【问题讨论】:

  • 导入路径是“github.com/gin-gonic/gin”,而不是“/home/ubuntu/goapi/src/github.com/roes/api/vendor/github.com/gin” -gonic/杜松子酒"
  • 是的,我不明白这部分。据我了解,它应该调用全局安装的包,我确实有“/home/ubuntu/goapi/src/github.com/roes/api/vendor/github.com/gin-gonic/gin”(下划线在前面)
  • 在您的文件夹结构中,您有github.com/rose。在您的错误消息中,您有github.com/roes。为什么?
  • 只有当您的代码显示 import "_/home/ubuntu/goapi/src/github.com/roes/api/vendor/github.com/gin-gonic/gin" 时才会出现该错误消息,这是错误的。否则,Go 不会查看 $GOPATH/src/_home/ubuntu/...
  • 有谁知道为什么会有下划线?请。我可以在正确的路径中找到杜松子酒,和上面一样只是不是下划线

标签: go go-gin


【解决方案1】:

如果项目根路径中没有启用 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

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    从 Go v1.11 开始,您可以使用 go modules,它可以让您使用 Go src/ 文件夹之外的模块创建项目。为此,您只需在根文件夹中运行命令go build

    这篇文章解释了一切:https://medium.com/mindorks/create-projects-independent-of-gopath-using-go-modules-802260cdfb51

    Gl & Hf :D

    【讨论】:

      【解决方案3】:

      也许我只是错过了项目文件夹中每个文件夹的这一步。 (很重要)

        mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_" 
      

      我一直在问我的朋友 Google,答案是:goVendor 非常依赖 $GOPHATH。 然后我用上面的命令创建控制器文件夹。所以每个文件夹也是如此。没问题:)

      感谢大家的帮助和观看

      【讨论】:

      • 是的,与 go vendor path 无关,问题仍然存在
      猜你喜欢
      • 2022-12-01
      • 2016-06-16
      • 2017-07-01
      • 2018-12-09
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      相关资源
      最近更新 更多