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 中搜索包。在 GOROOT 中找不到 controller/userhandler
之后,理想情况下,它应该在 GOPATH 中查找包,但它没有。
我已经将 GOPATH 设置为我的工作区路径,其中包括文件夹:bin
、src
和 pkg
.
$ go build main.go
main.go:5:2: package controller/userhandler is not in GOROOT (/usr/local/go/src/controller/userhandler)
最佳答案
运行 go mod init MODULE_NAME
(如果项目在 GOROOT 或 GOPATH 之外)或者只是简单地 go mod init
(如果项目在 GOROOT 或 GOPATH 中)。该命令应在项目的根文件夹上运行。这将创建一个 go.mod
文件,使 go 能够解析您的包。
关于go - 如何在导入包时在 GOPATH 中搜索包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62760999/