使用 Go 模块组织包中的本地代码 [英] Organize local code in packages using Go modules
问题描述
在 $GOPATH
= 1.11)时,我找不到将 main.go
中的一些代码分解到本地包中的方法>.
I can not find a way to factor out some code from main.go
into a local package when using Go modules (go version >= 1.11) outside of $GOPATH
.
我没有导入任何需要包含在 go.mod
中的外部依赖项,我只是想在本地组织这个 Go 模块的源代码.
I am not importing any external dependencies that need to be included into go.mod
, I am just trying to organize locally the source code of this Go module.
文件main.go
:
package main
// this import does not work
import "./stuff"
func main() {
stuff.PrintBaz()
}
文件./stuff/bar.go
(伪装成本地包):
The file ./stuff/bar.go
(pretending to be a local package):
package stuff
import "log"
type Bar struct {
Baz int
}
func PrintBaz() {
baz := Bar{42}
log.Printf("Bar struct: %v", baz)
}
文件go.mod
(命令go mod init foo
):
module foo
go 1.12
执行go run main.go
时:
- 如果我
import "./stuff"
,然后我看到build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff
. - 如果我
import "stuff"
,然后我看到build command-line-arguments: cannot load stuff: cannot find module provided package stuff
. - 如果我
导入带有包别名的东西./stuff"
,那么我再次看到:build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff
.
- If I
import "./stuff"
, then I seebuild command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff
. - If I
import "stuff"
, then I seebuild command-line-arguments: cannot load stuff: cannot find module providing package stuff
. - If I
import stuff "./stuff"
with a package alias, then I see again:build command-line-arguments: cannot find module for path _/home/<PATH_TO>/fooprj/stuff
.
我找不到让本地包与 go 模块一起工作的方法.
I can not find a way to make local packages work with go modules.
- 上面的代码有什么问题?
- 如何将本地包导入到使用 Go 模块(文件
go.mod
)定义的项目内的其他 Go 代码中?
- What's wrong with the code above?
- How can I import local packages into other Go code inside a project defined with Go modules (file
go.mod
)?
推荐答案
首先您必须为您的项目选择一个名称并将其写入 go.mod 文件.这个名字属于项目的根目录.您创建的每个新包都必须位于其自己的子目录中,并且其名称应与目录名称相匹配.
First you have to choose a name for your project and write it to go.mod file. This name belongs to root directory of the project. Each new package you create must be located inside its own subdirectory and its name should match directory name.
go.mod:
module myprojectname
或(首选方式,请参阅@典型182的下面的答案了解详情)
or (preferred way, see @typical182's answer below for details)
module github.com/myname/myproject
然后导入您的项目包,例如:
Then import your project's packages like:
import myprojectname/stuff
或
import github.com/myname/myproject/stuff
包stuff
的文件应该位于项目的stuff
目录中.您可以随意命名这些文件.
Files of package stuff
should be located inside project's stuff
directory. You name these files as you like.
还可以创建更深层次的项目结构.例如,您决定将源代码文件与其他文件(如应用程序配置、docker 文件、静态文件等)分开.让我们把 stuff
目录移动到 pkg
里面,pkg/stuff
里面的每个 go 文件仍然有 stuff
包名.导入东西包只需写:
Also it's possible to create deeper project structure. For instance, you decided to separate source code files from other ones (like app configs, docker files, static files, etc...). Let's move stuff
directory inside pkg
, every go file inside pkg/stuff
still have stuff
package name. To import stuff package just write:
import myprojectname/pkg/stuff
没有什么可以阻止您在层次结构中创建更多级别,例如 github.com/myuser/myproject/pkg/db/provider/postgresql
,其中:
Nothing stops you from creating more levels in the hierarchy like github.com/myuser/myproject/pkg/db/provider/postgresql
, where:
github.com/myuser/myproject
- 项目名称.postgresql
- 包名.pkg/db/provider/postgresql
- 相对于项目根目录的包路径.
github.com/myuser/myproject
- project name.postgresql
- package name.pkg/db/provider/postgresql
- path to the package relative to project's root.
您可以在此处阅读有关 go 模块的更多信息:https://github.com/golang/go/wiki/模块
You can read more about go modules here: https://github.com/golang/go/wiki/Modules
查看此存储库以获取有关项目组织中使用的各种模式的有用信息:https://github.com/golang-standards/project-layout 如果你进入 pkg
目录,你会发现哪些开源项目在它们的结构中使用了 pkg
目录.
Check out this repository to get useful information about various patterns used in project organizing: https://github.com/golang-standards/project-layout If you go inside pkg
directory you will find out which open source projects use pkg
directory in their structure.
这篇关于使用 Go 模块组织包中的本地代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!