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 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥15 为什么我通过html绘制的SVG折线图插入到word中坐标轴不显示出来
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 多目标优化算法在与其他算法数据对比结果判断