GoLang自定义包导入问题

huangapple go评论94阅读模式
英文:

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

  1. 通过编辑 go.mod 文件来修改模块路径:
- module main
+ module example.com/hello
  1. 修改导入路径:
  import (
-     "main/controllers"
+     "example.com/hello/controllers"

      "github.com/gin-gonic/gin"
  )
  1. controller.go 文件中移除尾部的 ;
- package controllers;
+ package controllers
  1. 将目录 contollers 重命名为 controllers,以匹配包名(r 被遗漏了)。

  2. 移除 vendor 文件夹。

解释

  1. 在 Go 中,main 有特殊的含义。引用自 Go 语言规范

> 通过将一个名为 main 包 的单个未导入包与其所有直接或间接导入的包链接起来,创建一个完整的程序。主包必须具有包名 main,并声明一个不带参数且不返回值的 main 函数。

  1. 不带点的导入路径保留给标准库和 Go 工具链使用。参见 cmd/go: document that module names without dots are reserved
英文:
  1. Modify the module path by editing go.mod:
- module main
+ module example.com/hello
  1. Modify the import path:
  import (
-     "main/controllers"
+     "example.com/hello/controllers"

      "github.com/gin-gonic/gin"
  )
  1. controller.go (remove trailing ;):
- package controllers;
+ package controllers
  1. rename the directory contollers to controllers to match the package name (r is missed).

  2. remove the vendor folder.

Explanation:

  1. 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.

  1. 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())
}

huangapple
  • 本文由 发表于 2023年4月14日 13:10:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76011773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定