简体   繁体   中英

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.

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. I just make it like MVC structure


I did remove go. and install it again flow Linux version https://golang.org/doc/install then

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. when I add controller folder with user.go with

 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

   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.

For example , a package named "/gin-gonic/gin" is missing or you cannot find this.

Please run this in src folder or parallel to github.com folder.

$ GO111MODULE=on go get -u github.com/gin-gonic/gin@v1.3.0
  1. I am assuming your $GOPATH is Users/YourUserName/go

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. To do so, you can simply run the command go build at your root folder.

This article explains it all: https://medium.com/mindorks/create-projects-independent-of-gopath-using-go-modules-802260cdfb51

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. then I Create the controller folder with above command. so every folder too. no problem :)

Thank you to everyone Helping and Viewing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM