英文:
Using binary packages directly
问题
我正在使用Go语言编写一个库。我计划进行分发,其中一个主要要求是“不包含源代码”。
为了进行测试,我创建了两个工作空间,如下所示:
WS1
- bin/
- pkg/linux_amd64/lib.a
- src/lib/src.go
WS2
- bin/
- pkg/
- src/main/main.go
我的第一个工作空间(WS1)是实际的虚拟库,其中包含一些实用函数。第二个工作空间(WS2)包含一个使用来自WS1的包(lib.a)的主函数。
一切都很顺利,直到我从WS1中删除了源代码。如果我删除WS1中的目录/lib/src.go,那么在进行go build时,我会收到以下错误:
main.go:5:2: cannot find package "lib" in any of:
/usr/local/go/src/pkg/lib (from $GOROOT) ../Testing/ws1/src/lib
(from $GOPATH)
上述消息告诉我们,我们应该保留源文件。仅预编译的二进制包无法直接使用。
根据网上的一些建议,我们可以保留一些带有时间戳值小于二进制包时间戳的虚拟源文件。但是,这对我们来说似乎不是一个可行的解决方案。如果虚拟源文件的时间戳不幸更新了会发生什么?
我在这里看到了类似的问题讨论:
https://github.com/golang/go/issues/2775
我的问题:
- 在Golang中,分发源代码是唯一的可能性吗?
- 为什么Go没有提供直接使用“.a”文件的方法?
- 如果在Go中保留源代码是强制性的,为什么没有在Go的任何地方提到这个小问题?(或者)我在这里漏掉了什么吗?
提前感谢你们的帮助!
英文:
I'm writing a library in Go. I'm planning to distribute it, and with a main requirement of 'without source codes'.
For testing, I have created two workspaces like following,
WS1
- bin/
- pkg/linux_amd64/lib.a
- src/lib/src.go
WS2
- bin/
- pkg/
- src/main/main.go
My first workspace (WS1) is the actual dummy library, which has some utility functions. Second workspace (WS2) has main function which uses the package (lib.a) from WS1.
Everything was working good until I remove the sources from WS1. If I remove the directory /lib/src.go in WS1, I'm getting the following error during go build,
> main.go:5:2: cannot find package "lib" in any of:
> /usr/local/go/src/pkg/lib (from $GOROOT) ../Testing/ws1/src/lib
> (from $GOPATH)
The above message indicates us that we should keep the source files as well. Precompiled binary packages alone cannot be used directly.
Based on few suggestions online, we may keep some dummy sources with timestamp value lesser than binary packages' timestamp. But, this doesn't seems to be a feasible solution for us. What happens if timestamp of the dummy sources got updated unfortunately?
I have seen similar issue discussed here,
https://github.com/golang/go/issues/2775
My Questions:
-
Distributing the sources is the only possibility in Golang?
-
Why Go is not providing a provision for using '.a' files directly?
-
If keeping the source is mandatory for Go, why this small thing is
not mentioned anywhere in Go? (or) Am I missing something here?
Thanks in advance for your help guys!
答案1
得分: 16
Go编译器只需要.a
文件。如果你发布了这些文件,任何人都可以在没有源代码的情况下使用你的包。
但是,你的用户将不得不手动调用编译器(例如6g
,而不是go
工具)。如果你发布了一个myfoo.a
文件,并且包含一个虚拟的源文件myfoo.go
,其中只包含package myfoo
和myfoo.a
的时间戳比myfoo.go
的时间戳新(并且你将所有文件放在正确的位置),那么你可以使用go
工具。
更新:较新版本的go工具会检测已删除的文件,并要求在src文件夹中存在所有文件(可能为空),这些文件具有正确的文件名和较旧的时间戳。管理时间戳不应该成为一个障碍。
不要被误导认为go
工具就是Go语言:它是一个非常方便的工具,用于构建、测试、获取等等你的Go代码,但它既不是语言,也不是编译器或链接器。
顺便说一下:没有分发源代码真的没有意义。
英文:
The Go compiler just needs the .a
files. If you ship them anybody will be able to use your package without the source code.
BUT your users will have to invoke the compiler (e.g. 6g
, not the go
tool) manually. If you ship a myfoo.a
file and a dummy source myfoo.go
containing just package myfoo
and the timestamp of myfoo.a
is newer than that of myfoo.go
(and you put everything in place) you may use the go
tool.
Update: Newer version of the go tool detect deleted files and require all files (possibly empty) with the proper filenames and older timestamps in the src folder.
Managing a timestamp should not be a dealbreaker.
Don't get fooled that the go
tool is Go: It is a dead convenient tool to build, test, get, whatever your Go code, but it is neither the language nor the compiler nor the linker.
BTW: There is really no point in not distributing the sources.
答案2
得分: 6
二进制包将在go1.7(2016年8月)中提供 - https://tip.golang.org/doc/go1.7
此版本添加了对使用仅二进制包构建程序的实验性、最小支持。这些包以二进制形式分发,没有相应的源代码。这个功能在某些商业环境中是必需的,但不打算完全整合到其余的工具链中。例如,假设可以访问完整源代码的工具将无法与这些包一起使用,并且没有计划在"go get"命令中支持这些包。
该提案位于https://github.com/golang/proposal/blob/master/design/2775-binary-only-packages.md,https://tip.golang.org/pkg/go/build/#hdr-Binary_Only_Packages提供了有关这个新功能的更多信息。
英文:
The binary-only packages will be available in go1.7 (August 2016) - https://tip.golang.org/doc/go1.7
> This release adds experimental, minimal support for building programs using binary-only packages, packages distributed in binary form without the corresponding source code. This feature is needed in some commercial settings but is not intended to be fully integrated into the rest of the toolchain. For example, tools that assume access to complete source code will not work with such packages, and there are no plans to support such packages in the “go get” command.
The proposal is at https://github.com/golang/proposal/blob/master/design/2775-binary-only-packages.md , https://tip.golang.org/pkg/go/build/#hdr-Binary_Only_Packages has more information about the new feature.
答案3
得分: 5
二进制包现在在Go 1.7中得到支持。
现在,您只能提供 .a 文件和没有源代码的伪造 Go 文件来分发它。
这里有一个Go1.7二进制包生成器的详细示例和脚本。
myframework/frameImplement.go
package myframework
import "fmt"
func Hello(name string) string {
return fmt.Sprintf("Hello, %s!", name)
}
main/main.go
package main
import (
"fmt"
"golang-binary-package-generator/myframework"
)
func main() {
fmt.Println(" start program ")
fmt.Println(" print program :", myframework.Hello("print something now"))
}
如果我想隐藏我的框架的源代码,只需使用 go build -i -o $GOPATH/pkg/framework.a
进行构建,然后修改您的源代码为
//go:binary-only-package
package framework
//you can add function prototype here for go doc etc, but no necessary.
这样,您可以使用我的二进制包生成器(脚本)来帮助您。
英文:
The binary-only packages is supported in go 1.7 now.
You can only provide .a files and fake go files without source code to distribute it now.
Here is a detailed example and a script of Go1.7 binary package generator.
myframework/frameImplement.go
package myframework
import "fmt"
func Hello(name string) string {
return fmt.Sprintf("Hello, %s!", name)
}
main/main.go
package main
import (
"fmt"
"golang-binary-package-generator/myframework"
)
func main() {
fmt.Println(" start program ")
fmt.Println(" print program :", myframework.Hello("print something now"))
}
If I want to hide my framework's source code, just build it with go build -i -o $GOPATH/pkg/framework.a
, then modify your source code to
//go:binary-only-package
package framework
//you can add function prototype here for go doc etc, but no necessary.
, which you can use my binary package generator(script) to help you.
答案4
得分: 1
决定放弃该功能:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论