Go1.7将支持二进制包分发
不出意外的话,Go1.7将在8月份发布。Go1.7最大的变化是引入SSA优化技术,号称有-10%
到30~40?%
的性能提升。
对于Windows用户,还有一个迟来的特性:就是生成C静态库(补充: Go1.7已经可生成静态库),然后可以用gcc工具将这个C静态库再打包为dll。 直接生成C接口的dll特性暂时还不确定能否完成。
不过我们这里主要关注另一个新特性:就是以二进制方式分发Go包。
为了保密源代码,已经出现了各种nb的技术,二进制发布虽然只是比较低级的一种方式, 但是总比没有好。
先看看相关的文档:
// Binary-Only Packages
//
// It is possible to distribute packages in binary form without including the
// source code used for compiling the package. To do this, the package must
// be distributed with a source file not excluded by build constraints and
// containing a "//go:binary-only-package" comment.
// Like a build constraint, this comment must appear near the top of the file,
// preceded only by blank lines and other line comments and with a blank line
// following the comment, to separate it from the package documentation.
// Unlike build constraints, this comment is only recognized in non-test
// Go source files.
//
// The minimal source code for a binary-only package is therefore:
//
// //go:binary-only-package
//
// package mypkg
//
// The source code may include additional Go code. That code is never compiled
// but will be processed by tools like godoc and might be useful as end-user
// documentation.
//
简单来说就是先将pkg打包为.a
文件,然后放入$GOPATH/pkg
目录的相应位置, 然后在$GOPATH/src
相应目录放入一个空的Go源文件,里面加入以下注释:
//go:binary-only-package
然后在 go build
的时候就会跳过重现编译代码的步骤,直接使用.a
文件了(.a
文件里面含有导入信息)。 当然,包的声明语句 package mypkg
还要保留。
在以二进制方式发布时,对应源文件中还是建议保留包的文档信息(不会build而已),这样便于用go doc
查阅包文档。
实际中,很多模块都有很多pkg组成,手工每个依赖的pkg会很麻烦, 估计需要一个专门的二进制包分发工具。