英文:
How to idiomatically change go github import paths?
问题
你可以使用goimports
工具来自动处理导入路径。goimports
是一个用于自动添加、删除和排序Go导入声明的工具。它会根据你的代码中实际使用的包来确定需要导入的包,并将它们按照一定的规则进行排序。
首先,你需要安装goimports
。你可以使用以下命令来安装它:
go get golang.org/x/tools/cmd/goimports
安装完成后,你可以在终端中使用goimports
命令来处理你的代码文件。例如,你可以在你的项目根目录下运行以下命令:
goimports -w .
这将会递归地处理当前目录下的所有Go代码文件,并自动更新导入路径。
注意,goimports
会根据你的$GOPATH
环境变量来确定导入路径的相对位置。所以在运行goimports
之前,你需要将你的项目放在$GOPATH/src
目录下,或者将$GOPATH
设置为你的项目根目录。
希望这可以帮助到你!如果你还有其他问题,请随时问我。
英文:
I have imported an app from github which has many imports, spread in several files like:
import (
"log"
"net/http"
"github.com/johndoe/sleekrest/model"
"github.com/johndoe/sleekrest/shared/passhash"
"github.com/johndoe/sleekrest/shared/recaptcha"
"github.com/johndoe/sleekrest/shared/session"
"github.com/johndoe/sleekrest/shared/view"
"github.com/johndoe/csrfbanana"
)
I want to work on the packages on my local path /go/src/myrest
, so I'd like to have imports to be like
import (
"log"
"net/http"
"./model"
"./shared/passhash"
"./shared/recaptcha"
"./shared/session"
"./shared/view"
"./csrfbanana"
)
I know I can use bash commands like sed
, find
, etc to replace the import paths, but I'm wondering if there is an idiomatic way to do so in golang?
答案1
得分: 2
这是一个关于在Go语言中使用相对导入路径的问题。在Go语言中,没有一种惯用的方法来实现这一点,因为相对导入路径在Go语言中并不常见。
不建议使用相对导入路径的原因如下:
根据《组织Go代码》的说明:
- 导入路径是用户导入包时使用的字符串。它指定了包源代码所在的目录(相对于
$GOROOT/src/pkg
或$GOPATH/src
)。 - 有时人们将
GOPATH
设置为源代码库的根目录,并将其包放在相对于库根目录的目录中,例如"src/my/package
"。一方面,这样可以使导入路径变短("my/package
"而不是"github.com/me/project/my/package
"),但另一方面,它会破坏go get
命令,并强制用户重新设置GOPATH
以使用该包。不要这样做。
根据《go命令》的说明:
- 其次,如果您在一个工作空间之外编译Go程序,可以在该程序的导入语句中使用相对路径来引用附近的代码,这样可以方便地在通常的工作空间之外尝试小型多包程序,但是这些程序无法使用
go install
命令进行安装(因为没有工作空间可以安装它们),因此每次构建时都会从头开始重新构建。为了避免歧义,Go程序不能在工作空间内使用相对导入路径。
此外,您可以参考以下StackOverflow答案:
关于供应商(Vendoring)的信息:
从Go 1.5开始,供应商成为一个实验性功能。从Go 1.6开始,供应商不再是实验性功能。有关供应商的更多信息,请参阅:
- go命令的供应商目录部分。
- Russ Cox的Go 1.5供应商实验文档。
英文:
There is not an idiomatic way to do this because relative import paths are not idiomatic Go.
Reasons to not use relative import paths
The following is from Organizing Go code:
> An import path is the string with which users import a package. It specifies the directory (relative to $GOROOT/src/pkg
or $GOPATH/src
) in which the package's source code resides.
>
> Sometimes people set GOPATH
to the root of their source repository and put their packages in directories relative to the repository root, such as "src/my/package
". On one hand, this keeps the import paths short ("my/package
" instead of "github.com/me/project/my/package
"), but on the other it breaks go get and forces users to re-set their GOPATH
to use the package. Don't do this.
The following is from Command go:
> Second, if you are compiling a Go program not in a work space, you can use a relative path in an import statement in that program to refer to nearby code also not in a work space. This makes it easy to experiment with small multipackage programs outside of the usual work spaces, but such programs cannot be installed with go install
(there is no work space in which to install them), so they are rebuilt from scratch each time they are built. To avoid ambiguity, Go programs cannot use relative import paths within a work space.
Also, you might look at these StackOverflow answers:
- zzzz's answer to Golang how to import local packages without go path?
- mostafa's answer to Relative import from parent directory
Vendoring
Vendoring became an experimental feature in Go 1.5. As of Go 1.6, vendoring is no longer experimental. For more information on vendoring, see:
- Vendor Directories section of Command go.
- Go 1.5 Vendor Experiment document by Russ Cox.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论