Home:ALL Converter>how to distribute go binary only package

how to distribute go binary only package

Ask Time:2018-05-20T10:52:10         Author:Minusy

Json Formatter

I want to distribute packages in binary form without including the source code.

my demo project directory structure is like this:

demo
├── greet
│   ├── greet.go
│   └── hi
│       └── hi.go
├── hello
│   └── hello.go
└── main.go

main.go:

package main

import (
    "fmt"
    "demo/greet"
    "demo/hello"
)

func main(){
    fmt.Println("greet:")
    greet.Greet()

    fmt.Println("hello:")
    hello.Hello()
}

greet.go

package greet

import (
    "demo/greet/hi"
    "fmt"
)

func Greet(){
    fmt.Println("Greet Call Hi")
    hi.Hi()
}

hi.go

package hi

import "fmt"

func Hi(){
    fmt.Println("Hi")
}

hello.go

package hello

import (
    "fmt"
    "demo/greet"
)

func Hello(){
    fmt.Println("hello call greet")
    greet.Greet()
}

And I do this:

[user@localhost greet]$ go install -a ./...

It generated greet.a and greet/hi.a in $GOPATH/pkg/linux_amd64/demo. Then I edit hi.go and greet.go.

[user@localhost greet]$ cat greet.go
//go:binary-only-package

package greet
[user@localhost greet]$ cat hi/hi.go
//go:binary-only-package

package hi

Then I run main.go, I get errors:

[user@localhost greet]$ cat hi/hi.go
# command-line-arguments
cannot find package demo/greet/hi (using -importcfg)
/home/user/go/pkg/tool/linux_amd64/link: cannot open file : open : no such file or directory

greet is the package I want to distribute. If I delete package hi, main.go can run well.

demo
├── greet
│   └── greet.go
├── hello
│   └── hello.go
└── main.go

install:

[user@localhost greet]$ go install .
[user@localhost greet]$ vim greet.go
//go:binary-only-package

package greet
[user@localhost greet]$ cd ..
[user@localhost demo]$ go run main.go
greet:
Greet ...
hello:
hello call greet
Greet ...
[user@localhost demo]$ 

So my main problem is: how can I build binary lib and others can't see my source code. And the package has many sub-package.(If the package haven't sub-package, I use //go:binary-only-package method can work well)

Please help or try to give some ideas how to solve this. Thanks in advance.

Author:Minusy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50431251/how-to-distribute-go-binary-only-package
VonC :

Note (2019): binary-only packages won't be supported much longer.\n\nAs the Go 1.12 (February 2019) release notes states (following CL 152918):\n\n\n Binary-only packages\n \n Go 1.12 is the last release that will support binary-only packages. \n\n\nThis is detailed in Go issue 28152:\n\n\n Binary-only packages are increasingly hard to support safely. \n \n There is no guarantee that the compilation of the binary-only package used the same versions of the dependencies that the final link does (and it would probably be too onerous to insist on that).\n As a result, the binary-only package may have been compiled using escape analysis results or inline function bodies for dependencies that are no longer accurate.\n The result can be silent memory corruption.\n \n My memory is that we added binary-only packages originally so that a professor could hand out binary solution sets so that a student who didn't finish lab 2 could still move on to lab 3.\n I don't know why else anyone uses them anymore, but they're probably going to break more and more as the compiler gets more sophisticated.\n\n\nThis was also discussed here.",
2019-01-13T00:18:31
yy