英文:
GoLang custom package import issue
问题
我正在学习Go语言,并遇到了一个问题。
我使用go mod init main
命令创建了mod文件。
接下来,我创建了一个包含以下内容的controllers和routes文件夹:
├── contollers
│ └── users.controller.go
├── routes
│ ├── index.go
│ └── users.routes.go
├── vendor
│ └── modules.txt
├── go.mod
├── go.sum
└── main.go
在mod文件中,模块的内容如下所示:
module main
现在,当我尝试将controller导入到router中时,出现了导入错误。
我尝试了以下几种方法。
尝试1:
import (
"$GOPATH/controllers"
"github.com/gin-gonic/gin"
)
它给出了invalid import path: "$GOPATH/controllers"syntax
错误。
尝试2:
import (
"$GOPATH/main/controllers"
"github.com/gin-gonic/gin"
)
错误仍然相同。
尝试3:
import (
"main/controllers"
"github.com/gin-gonic/gin"
)
controller.go
package controllers;
import (
"fmt"
"github.com/gin-gonic/gin"
)
func HealthCheck() gin.HandlerFunc {
return func (c *gin.Context) {
fmt.Println("reached controller")
}
}
router.go
package routes
import (
"bootcamp.com/server/controllers"
"github.com/gin-gonic/gin"
)
func UserRouters(inComingRoutes *gin.Engine) {
inComingRoutes.GET("/api/health", controllers.HealthCheck());
}
它会抛出以下错误:could not import main/controllers (no required module provides package "main/controllers")
我被困在这个问题上已经3-4个小时了,有人可以建议我如何将controller导入到我的路由中吗?
提前感谢。
英文:
I'm learning GoLang, and got into an issue.
I created the mod file with go mod init main
Next I created controller and Routes folder which looks something like below:
├── contollers
│   └── users.controller.go
├── routes
│   ├── index.go
│   └── users.routes.go
├── vendor
│ └── modules.txt
├── go.mod
├── go.sum
└── main.go
in the mod file the module looks like something like this
module main
Now when I'm trying to import controller into router, it's giving me import error.
I been doing below things.
Attempt - 1
import (
"$GOPATH/controllers"
"github.com/gin-gonic/gin"
)
it's giving invalid import path: "$GOPATH/controllers"syntax
error
Attempt - 2
import (
"$GOPATH/main/controllers"
"github.com/gin-gonic/gin"
)
again the error is same
Attempt - 3
import (
"main/controllers"
"github.com/gin-gonic/gin"
)
controller.go
package controllers;
import (
"fmt"
"github.com/gin-gonic/gin"
)
func HealthCheck() gin.HandlerFunc {
return func (c *gin.Context) {
fmt.Println("reached controller")
}
}
router.go
package routes
import (
"bootcamp.com/server/controllers"
"github.com/gin-gonic/gin"
)
func UserRouters(inComingRoutes *gin.Engine) {
inComingRoutes.GET("/api/health", controllers.HealthCheck());
}
it's throwing error like this, could not import main/controllers (no required module provides package "main/controllers")
I been stuck with this for 3-4 hours, can anyone please suggest me how can I import that controller into my route.
Thanks in advance.
答案1
得分: 2
- 通过编辑
go.mod
文件来修改模块路径:
- module main
+ module example.com/hello
- 修改导入路径:
import (
- "main/controllers"
+ "example.com/hello/controllers"
"github.com/gin-gonic/gin"
)
controller.go
文件中移除尾部的;
:
- package controllers;
+ package controllers
-
将目录
contollers
重命名为controllers
,以匹配包名(r
被遗漏了)。 -
移除
vendor
文件夹。
解释:
- 在 Go 中,
main
有特殊的含义。引用自 Go 语言规范:
> 通过将一个名为 main 包 的单个未导入包与其所有直接或间接导入的包链接起来,创建一个完整的程序。主包必须具有包名 main,并声明一个不带参数且不返回值的 main 函数。
- 不带点的导入路径保留给标准库和 Go 工具链使用。参见 cmd/go: document that module names without dots are reserved。
英文:
- Modify the module path by editing
go.mod
:
- module main
+ module example.com/hello
- Modify the import path:
import (
- "main/controllers"
+ "example.com/hello/controllers"
"github.com/gin-gonic/gin"
)
controller.go
(remove trailing;
):
- package controllers;
+ package controllers
-
rename the directory
contollers
tocontrollers
to match the package name (r
is missed). -
remove the
vendor
folder.
Explanation:
main
has a special meaning in go. Quotes from the golang spec:
> A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.
- Import paths without dots are reserved for the standard library and go toolchain. See cmd/go: document that module names without dots are reserved.
答案2
得分: -1
使用以下命令:
go mod tidy
修改你的users.routes.go
文件:
package routes
import (
"main/controllers"
"github.com/gin-gonic/gin"
)
func UserRouters(inComingRoutes *gin.Engine) {
inComingRoutes.GET("/api/health", controllers.HealthCheck())
}
英文:
use this command
go mod tidy
Modify your users.routes.go
package routes
import (
"main/contollers"
"github.com/gin-gonic/gin"
)
func UserRouters(inComingRoutes *gin.Engine) {
inComingRoutes.GET("/api/health", controllers.HealthCheck())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论