英文:
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
如果你只需要构建logging
或db-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 <INSERT_MODULE_NAME_FROM_GO_MOD>/logging
go build <INSERT_MODULE_NAME_FROM_GO_MOD>/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
会按需重新构建目标文件,并将其存储在单独的构建缓存中。
如果你想要分发一个非公开的模块供其他模块使用,有几种选择:
-
你可以将模块发布到一个私有仓库中,通常使用存储在
.netrc
文件中的凭据进行访问,并使用GOPRIVATE
环境变量告诉go
命令不要尝试从公共代理和校验和服务器获取它。 -
你可以提供一个私有的
GOPROXY
服务器或包含源代码的目录,并使用GOPROXY
环境变量指示go
命令使用该代理。 -
你可以将源代码发布为一个目录树,并在使用者的
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:
-
You can publish the module to a private repository — typically accessed with credentials stored in a
.netrc
file — and use theGOPRIVATE
environment variable to tell thego
command not to try to fetch it from the public proxy and checksum servers. -
You can provide a private
GOPROXY
server or directory containing the source code, and use theGOPROXY
environment variable to instruct thego
command to use that proxy. -
You can publish the source code as a directory tree and use a
replace
directive in the consumer'sgo.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论