模块化代码在 Go 中如何工作?
由于没有 C/编译语言背景,我发现很难掌握使用 Go 的包机制来创建模块化代码。
在 Python 中,要导入模块并访问其函数等,这是一个简单的情况,
import foo
其中 foo.py 是要在同一目录中导入的模块的名称。否则,您可以将空的 __init__.py
添加到子文件夹中并通过以下方式访问模块
from subfolder import foo
然后您可以通过简单地通过模块名称引用它们来访问函数,例如 y = foo.bar(y)
。这使得将逻辑代码片段彼此分离变得很容易。
然而,在 Go 中,您在源文件本身中指定包名称,例如
package foo
在“foo”模块的顶部,然后您可以通过该模块导入
import (
"foo"
)
并通过它引用它,即 y := foo。条(x) 。但我无法理解的是这在实践中是如何运作的。 golang.org 上的相关文档看起来很简洁,并且针对具有更多(任何)使用 makefile 和编译器经验的人。
有人可以清楚地解释一下如何在 Go 中模块化代码、正确的项目结构以及编译过程如何工作吗?
Not having come from a C/compiled languages background, I'm finding it hard to get to grips with using Go's packages mechanism to create modular code.
In Python, to import a module and get access to it's functions and whatnot, it's a simple case of
import foo
where foo.py is the name of the module you want to import in the same directory. Otherwise you can add an empty __init__.py
into a subfolder and access the modules via
from subfolder import foo
You can then access functions by simply referencing them through the module name, e.g. y = foo.bar(y)
. This makes it easy to separate logical pieces of code from one another.
In Go however, you specify the package name in the source file itself, e.g.
package foo
at the top of the 'foo' module, which you can then supposedly import through
import (
"foo"
)
and then refer to it through that, i.e. y := foo.Bar(x)
. But what I can't wrap my head around is how this works in practice. The relevant docs on golang.org seem terse, and directed to people with more (any) experience using makefiles and compilers.
Can someone please clearly explain how you are meant to modularise your code in Go, the right project structure to do so, and how the compilation process works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
维基百科的答案,欢迎补充/编辑。
模块化
同一个包中的多个文件
package
指令开头,这意味着 Go 将它们视为一大堆代码。您可以从b.go
透明地调用a.go
中的函数。这主要是为了代码组织的好处。blog.go
(主文件)、entry.go
和server 进行布局.go
。由你决定。虽然您可以在一个大文件中编写博客包,但这往往会影响可读性。多个包
$GOROOT
中。您编写的任何程序都可以导入“”
,然后调用blog.go
和news.go
都会导入“server”
。编译
我目前使用 gomake 和 Makefiles。 Go 安装附带了一些很棒的 make 包含文件,可以简化包或命令的创建。这并不难,快速了解这些内容的最佳方法就是查看开源项目中的示例 makefile 并阅读“如何编写 Go 代码”。
Wiki answer, please feel free to add/edit.
Modularization
Multiple files in the same package
package <name>
directive means that they are treated as one big set of code by Go. You can transparently call functions ina.go
fromb.go
. This is mostly for the benefit of code organization.blog.go
(the main file),entry.go
, andserver.go
. It's up to you. While you could write a blog package in one big file, that tends to affect readability.Multiple packages
$GOROOT
. Any program you write canimport "<name>"
and then call<name>.someFunction()
blog.go
andnews.go
would bothimport "server"
.Compilation
I currently use gomake with Makefiles. The Go installation comes with some great include files for make that simplify the creation of a package or a command. It's not hard and the best way to get up to speed with these is to just look at sample makefiles from open source projects and read "How to Write Go Code".
除了包组织之外,像Python中的pip一样,使用dep https://github.com/golang/dep< /a> 用于 go 包管理。如果您在现有的 go 包上使用它,它将自动构建依赖关系树,其中包含所有正在使用的包的版本。当转移到生产服务器时,dep Ensure将使用Gopkg.toml来安装所有必需的软件包。
只需使用 dep Ensure -add ,dep 的其他命令是:
In addition to the package organisation, Like pip in python, use dep https://github.com/golang/dep for go package management. if you use it on existing go package it will automatically build the dependency tree with versions for all the packages being used. when shifting to production server, dep ensure will use Gopkg.toml to install all the required packages.
Just use dep ensure -add , other commands for dep are: