模块化代码在 Go 中如何工作?

发布于 2024-12-02 01:55:03 字数 711 浏览 4 评论 0原文

由于没有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2024-12-09 01:55:03

维基百科的答案,欢迎补充/编辑。

模块化

  1. 同一个包中的多个文件

    • 这就是听起来的样子。同一目录中的一堆文件都以相同的 package 指令开头,这意味着 Go 将它们视为一大堆代码。您可以从 b.go 透明地调用 a.go 中的函数。这主要是为了代码组织的好处。
    • 一个虚构的示例是“blog”包,可能会使用 blog.go(主文件)、entry.goserver 进行布局.go。由你决定。虽然您可以在一个大文件中编写博客包,但这往往会影响可读性。
  2. 多个包

    • 标准库就是这样完成的。基本上,您创建模块并选择将它们安装到 $GOROOT 中。您编写的任何程序都可以导入“,然后调用.someFunction()
    • 实际上,任何独立或共享组件都应该编译成包。回到上面的博客包,如果你想添加新闻提要,你可以将 server.go 重构为包。然后 blog.gonews.go 都会导入“server”

编译

我目前使用 gomake 和 Makefiles。 Go 安装附带了一些很棒的 make 包含文件,可以简化包或命令的创建。这并不难,快速了解这些内容的最佳方法就是查看开源项目中的示例 makefile 并阅读“如何编写 Go 代码”

Wiki answer, please feel free to add/edit.

Modularization

  1. Multiple files in the same package

    • This is just what it sounds like. A bunch of files in the same directory that all start with the same package <name> directive means that they are treated as one big set of code by Go. You can transparently call functions in a.go from b.go. This is mostly for the benefit of code organization.
    • A fictional example would be a "blog" package might be laid out with blog.go (the main file), entry.go, and server.go. It's up to you. While you could write a blog package in one big file, that tends to affect readability.
  2. Multiple packages

    • The standard library is done this way. Basically you create modules and optionally install them into $GOROOT. Any program you write can import "<name>" and then call <name>.someFunction()
    • In practice any standalone or shared components should be compiled into packages. Back to the blog package above, If you wanted to add a news feed, you could refactor server.go into a package. Then both blog.go and news.go would both import "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".

五里雾 2024-12-09 01:55:03

除了包组织之外,像Python中的pip一样,使用dep https://github.com/golang/dep< /a> 用于 go 包管理。如果您在现有的 go 包上使用它,它将自动构建依赖关系树,其中包含所有正在使用的包的版本。当转移到生产服务器时,dep Ensure将使用Gopkg.toml来安装所有必需的软件包。
只需使用 dep Ensure -add ,dep 的其他命令是:

Commands:

    init     Set up a new Go project, or migrate an existing one
    status   Report the status of the project's dependencies
    ensure   Ensure a dependency is safely vendored in the project
    version  Show the dep version information
    check    Check if imports, Gopkg.toml, and Gopkg.lock are in sync

 Examples:
    dep init                               set up a new project
    dep ensure                             install the project's dependencies
    dep ensure -update                     update the locked versions of all dependencies
    dep ensure -add github.com/pkg/errors  add a dependency to the project

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:

Commands:

    init     Set up a new Go project, or migrate an existing one
    status   Report the status of the project's dependencies
    ensure   Ensure a dependency is safely vendored in the project
    version  Show the dep version information
    check    Check if imports, Gopkg.toml, and Gopkg.lock are in sync

 Examples:
    dep init                               set up a new project
    dep ensure                             install the project's dependencies
    dep ensure -update                     update the locked versions of all dependencies
    dep ensure -add github.com/pkg/errors  add a dependency to the project
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文