douren7921 2015-02-09 09:08
浏览 67
已采纳

直接使用二进制包

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:

  1. Distributing the sources is the only possibility in Golang?

  2. Why Go is not providing a provision for using '.a' files directly?

  3. 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!

  • 写回答

3条回答 默认 最新

  • douxie1957 2015-02-09 10:13
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?