导入软件包时如何在GOPATH中搜索软件包?

古尔然·辛格:
package main

import (
    "fmt"
    "controller/userhandler"   //not able to import this custom package      
    "github.com/gin-gonic/gin"                                         
    "net/http"                                                         
    "github.com/jinzhu/gorm"                                               
  _ "github.com/jinzhu/gorm/dialects/postgres"
)

func main(){
}

下面的代码显示Go仅在GOROOT中搜索软件包。理想情况下,controller/userhandlerGOROOT中找不到后,它应该在GOPATH中查找软件包,但不会。

我已经设置GOPATH到我的工作空间路径,包括文件夹:binsrc,和pkg

$ go build main.go                                                           
main.go:5:2: package controller/userhandler is not in GOROOT (/usr/local/go/src/controller/userhandler)
Fanan Dala:

运行go mod init MODULE_NAME(如果项目在GOROOTGOPATH外部)或只是简单地go mod init(如果项目在GOROOT或GOPATH内部)。该命令应在项目的根文件夹上运行。这将创建一个go.mod文件,文件将使能够解析您的软件包。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章