Go中的import路径在版本化的包中是如何工作的?

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

How does Go import Path works in versioned packages

问题

我对Golang如何解析命名导入感到好奇。

在这个例子中,我使用Echo作为我的应用程序的包。

package main

import (
    "net/http"

    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    e.Logger.Fatal(e.Start(":1323"))
}

如你所见,在import行中,Echo实际上是通过它的“v4”版本进行引用的,但Go可以解析为“echo”。
我在Echo的存储库中查找了一下,没有找到关于Go如何解析这个的明确说明。

PS:在过去,我使用了别名的方式,例如:

...
import (
    echo "github.com/labstack/echo/v4"
)
...

但这似乎是一种变通方法。

英文:

I got kinda curious about how does Golang resolves named imports.

In example here, I got Echo as a package for my app.

package main

import (
    "net/http"

    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    e.Logger.Fatal(e.Start(":1323"))
}

As seen on the import line, Echo is actually being referenced by it's "v4" version, but Go can resolve as "echo".
I've dug around Echo's Repo and couldn't find anything explicit on how does Go can resolve this.

PS: On the past I've used it with an alias, such as:

...
import (
    echo "github.com/labstack/echo/v4"
)
...

but that seems to be a workaround.

答案1

得分: 1

Go文件的第一行使用package指令声明包名。当import不是别名导入时,该名称将被解析为导入的名称。当你需要区分具有相同包名但不同导入路径的多个包时,可以使用别名导入。

go.mod文件保存了Go包的导入路径(对于echo来说,导入路径是github.com/labstack/echo/v4)。正如JimB所说,包名不需要与导入路径对应,这只是一种约定。

rsc.io/quote(源代码位于https://github.com/rsc/quote)解释了Go包的版本控制和导入。你还可以查看Russ Cox的这些博文,解释了Go中的包版本控制。

英文:

The first line of a Go file declares the package name using the package directive. This is the name that import resolves to when it is not an aliased import. You can use aliased imports when you need to disambiguate between multiple packages that have the same package name but different import paths.

The go.mod file keeps the import path of the Go package (for echo that is github.com/labstack/echo/v4). As JimB said, the package name does not need to correspond to the import path, it only does so by convention.

rsc.io/quote (source code at https://github.com/rsc/quote) explains Go package versioning and imports. You can also check these blog posts by Russ Cox explaining package versioning in Go.

答案2

得分: 0

当你导入一个包时,名称将是包名而不是文件夹名

在你的情况下,如图所示,文件夹名是v4,但包名是echo,因此它将被解析为echo

这是一个好的和推荐的约定,使用与文件夹名相同的包名,

但是对于主要版本的文件夹进行版本控制是被认可和有效的。

Go中的import路径在版本化的包中是如何工作的?

英文:

When you import a package, the name will be the package name not the folder name.

In your case, as shown in the picture, the folder name is v4 but the package name is echo, so it will be resolved as echo.

Go中的import路径在版本化的包中是如何工作的?

It's a good and recommended convention to use the same package name as your folder name,

But folder versioning for major releases is known and valid.

huangapple
  • 本文由 发表于 2023年1月4日 23:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75007557.html
匿名

发表评论

匿名网友

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

确定