dongyudun6465 2018-05-20 02:52
浏览 91
已采纳

如何分发仅二进制包

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.

  • 写回答

2条回答 默认 最新

  • doumibi6899 2018-05-20 06:46
    关注

    "go run" will compile and run the given main package. So it will check the timestamps of your source files and recompile them if it sees they are newer than the previously compiled libraries (.a files).

    It will skip this step for binary only packages. So in this case it will check for any given package that there is just a single source file with the text //go:binary-only-package. That is how the feature is specified. When it sees that, it will use the .a files only.

    When you have a subpackage with a source file in it, it seems that files in there end up counting as part of the enclosing root package and the requirement of having just a single source file is broken. So then "go run" will fail. I suspect that is why it doesn't work. And that is why deleting the hi subpackage causes it to work.

    The intended use case for binary-only is that you compile your package, and then when you make it available to others you do not include any source files, just the .a files. But for this to work you need to be able to tell other peoples' compilers that your package is just the .a files. For that you need to include just the one source file with the text //go:binary-only-package. And you've basically shown here that for a package that has subpackages, the whole thing must have just a single such source file, there can be only one, you cannot have more of them in the subpackage folders. So just provide the one. That is how the feature works. You provide the .a files and just that one source file to others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败