Go1.7将支持二进制包分发

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会很麻烦, 估计需要一个专门的二进制包分发工具。


https://chai2010.cn/

转载于:https://my.oschina.net/chai2010/blog/666411

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值