Publish the Go package in binary form

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

When we use go for development, we often use third-party libraries, when we generally go get go to github.com, bitbucket, or our own private library to pull the third library source code. Today, just a group of netizens asked if they can develop their own library in binary form to provide users, I just sort of a bit.

The motivation for providing a library in binary mode may be to protect your company's intellectual property, or it may be from a security standpoint, avoiding leaks of key information, and so on, which is not the scope of this article.

Although the previous version was able to use the binary library in a tortuous implementation, the formal offer was in go 1.7. In Go 1.7 Release notes mentions:

This release adds experimental, minimal-building programs using binary-only packages, packages distributed in binary form without the corresponding source code. This feature was needed in some commercial settings it is not intended to being fully integrated into the rest of the Toolcha In. For example, tools this assume access to complete source code won't work with such packages, and there is no plans to Support such packages in the "Go get" command.

There are also relevant descriptions in the package build documentation:

It is possible to distribute packages in binary form without including the source code used for compiling the package. To does this, the package must is distributed with a source file not excluded by build constraints and containing a "//go:bi Nary-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 co Mments and with a blank line following the comment, to separate it from the package documentation. Unlike build constraints, this comment are only recognized in Non-test Go source files.

The minimal source code for a binary-only package is therefore:

1234
//go:binary-only-package  Package Mypkg>

The source code may include additional Go code. That code was never compiled but would be processed to tools like godoc and might be useful as end-user documentation.

You only need to provide a compiled library and provide a source file for the package. This source file does not contain any code logic, just add //go:binary-only-package instructions (note // and go:... do not add spaces). This allows users to use your binary library directly when they use it.

Of course, for user convenience, such as some tools and IDE support, you can exported define the type into this file. Also, simply provide some "fake" implementations, just for the IDE tool to know the type that the package contains exported .

The implementation of this feature was implemented by Russ Cox in both commits: cl#22432, cl#22433, and he submitted a design document: Proposal:binary-only package.

Related discussions in: #12186, #2775.

Let's take a look at how to use this example.

Here is a package that implements our business logic, which provides constants, variables, functions, and struct types to compile it x.a .

123456789101112131415161718192021
 Package ximport"FMT"const PI = 3.14var (x = +)  Func init () {FMT. Println ("x init")}funcintint {return x + y}type struct int}

This is the core asset of our company, it is sold by this bank, so we do not want to expose this source code. Then we can provide the user with a compile x.a , and provide an dummy implementation:

1234567891011121314151617
//go:binary-only-package  package xconst PI = 0.0var (x = 0)funcintint {return  0}typestructint}

The user gets this and puts it in the x.a $GOPATH/pkg appropriate package and puts the go file so that the $GOPATH/src user can use the library properly.

Of course, you'd better pack your library into a zip so that the user can easily copy into the unzip $GOPATH .

With Golang binary-only packages can also search some related tutorials, someone also wrote a sample example: Tcnksm/go-binary-only-package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.