构建没有主文件的Go模块

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

Building Go module without main file

问题

我有一个包含一些共享代码的小模块。该模块的结构如下:

Shared
├── go.mod
├── go.sum
├── logging
│   └── logger.go
└── db-utils
    ├── db.go

如果我尝试从该目录下构建Shared模块,会出现一个错误,提示该模块下没有可用的Go文件。

bash-3.2$ go build .
no Go files in /Users/xxxx/go/src/Shared

有没有一种方法可以构建一个只包含包而没有main.go文件的Go模块?这样做的动机是为了在其他无法访问互联网或私有仓库的Go模块中使用这些包。

英文:

I have a small module that contains some shared code. The module looks like the following :

Shared
├── go.mod
├── go.sum
├── logging
│   └── logger.go
└── db-utils
    ├── db.go

If I'll try to build the Shared module from inside the directory I'm getting an error that no go file available under this module.

bash-3.2$ go build .
no Go files in /Users/xxxx/go/src/Shared

Is there a way to build a Go module that only has packages inside and doesn't have a main.go file? The motivation here is to use those packages in other go modules that can't access the internet or private repo in order to retrieve the packages.

答案1

得分: 5

如果你只需要构建loggingdb-utils目录中的文件,那么你可以在根目录Shared中执行以下命令:

go build <INSERT_MODULE_NAME_FROM_GO_MOD>/logging
go build <INSERT_MODULE_NAME_FROM_GO_MOD>/db-utils

我不确定如果其中一个文件依赖于另一个目录中的文件,这些命令是否会起作用。

另一个可能会构建整个项目的命令是:

go build ./...
英文:

If you only needed to build files in either the logging or db-utils directory, then you could executing the following from the root directory Shared:

go build &lt;INSERT_MODULE_NAME_FROM_GO_MOD&gt;/logging
go build &lt;INSERT_MODULE_NAME_FROM_GO_MOD&gt;/db-utils

I'm not certain if those commands will work if one of the files has a dependency on a file from the other directory.

Another command that will probably build the entire project is this:

go build ./...

答案2

得分: 4

go命令将下载的模块存储在模块缓存中,以源代码的形式而不是编译后的目标文件。go build会按需重新构建目标文件,并将其存储在单独的构建缓存中。

如果你想要分发一个非公开的模块供其他模块使用,有几种选择:

  1. 你可以将模块发布到一个私有仓库中,通常使用存储在.netrc文件中的凭据进行访问,并使用GOPRIVATE环境变量告诉go命令不要尝试从公共代理和校验和服务器获取它。

  2. 你可以提供一个私有的GOPROXY服务器或包含源代码的目录,并使用GOPROXY环境变量指示go命令使用该代理。

  3. 你可以将源代码发布为一个目录树,并在使用者的go.mod文件中使用replace指令将其插入到模块图中。

英文:

The go command stores downloaded modules in the module cache as source code, not compiled object files. go build rebuilds the object files on demand and stores them in a separate build cache.

If you want to distribute a non-public module for use with other modules, you have a few options:

  1. You can publish the module to a private repository — typically accessed with credentials stored in a .netrc file — and use the GOPRIVATE environment variable to tell the go command not to try to fetch it from the public proxy and checksum servers.

  2. You can provide a private GOPROXY server or directory containing the source code, and use the GOPROXY environment variable to instruct the go command to use that proxy.

  3. You can publish the source code as a directory tree and use a replace directive in the consumer's go.mod file to slot it into the module graph.

答案3

得分: 2

有没有一种方法可以构建一个只包含包而没有main.go文件的Go模块?

没有。构建过程的输入是一个包,而不是一个模块。请注意,在go build的CLI文档中,它说的是[packages]

当构建一个包导致多个包被编译时,这只是直接和间接导入语句来自正在构建的包中的.go文件的结果。

请注意,Go不支持将包编译为二进制文件以分发闭源库等。虽然不总是这样,但这并不总是这样。请参见[#28152]和[Binary-Only packages]。最接近支持这一点的是[插件],但它们还在开发中,并且需要在运行时解析符号。

英文:

> Is there a way to build a go module that only has packages inside and doesn't have a main.go file?

No. The input for the build process is a package, not a module. Note how it says [packages] in the CLI documentation of go build.

When building a package leads to multiple packages being compiled, that is merely a consequence of direct and indirect import statements coming from .go-files located in the package you are building.

Note that Go does not support compiling packages to binaries to distribute closed-source libraries or such. This was not always the case, though. See #28152 and Binary-Only packages. The closest which exists to supporting that are plugins, but they are a work in progress and require resolution of symbols at runtime.

huangapple
  • 本文由 发表于 2021年6月22日 16:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/68080035.html
匿名

发表评论

匿名网友

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

确定