How to use/distribute a binary package without Go source code

huangapple go评论81阅读模式
英文:

How to use/distribute a binary package without Go source code

问题

基本上,我的文件夹结构如下:

bin/
pkg/
    linux_amd64/
        github.com/user/
            stringutil.a
src/
    github.com/user/
        hello/
            hello.go(主文件)
        stringutil/
            stringutil.go

hello.go 是主程序,它从 stringutil.go 中导入一些函数。

运行 go install 后,stringutil.a 将在 pkg 目录下创建(如上所示)。

我的问题是,既然我已经有了 stringutil.a,我可以再次运行 go install 吗,而不需要 stringutil.go 源文件?

我是一名Java程序员,对Go还不熟悉。通常在Java中,在编译过程中,我们可以将编译后的jar文件导入到类路径中并进行编译。

在Go中,我该如何做同样的事情?如果我有一个已编译的包 stringutil.a,我该如何将这个文件分发给其他人,并让他们在不暴露原始源代码的情况下使用它?

英文:

Basically my folder structure is as below:

bin/               
pkg/
    linux_amd64/         
        github.com/user/
            stringutil.a  
src/
    github.com/user/
        hello/
            hello.go   (main file)  
        stringutil/
            stringutil.go

hello.go is the main program that imports some function from stringutil.go.

After running go install, stringutil.a is created under pkg (as above).

My question is, can I run go install again without stringutil.go source since I already have stringutil.a?

I'm a Java programmer and new to Go. Usually in Java, during compilation, we can import the compiled jar file to the classpath and do compilation.

How can I do the same thing in Go? If I have a compiled package, stringutil.a, how can I distribute this file to someone and let them use it without exposing the original source code?

答案1

得分: 4

你将编译好的包放错了文件夹。

在你的Go工作空间的pkg文件夹中,编译好的包对象按照目标架构进行组织,例如linux_amd64windows_amd64。在其中,按照父包的路径进行排列,直接的父文件夹就是放置编译好的包对象的位置。

检查一下go install将你的包对象放在哪里,并复制相同的文件夹结构。

例如,如果你的包是path/to/parent/package/abc,并且你编译为Linux 64位,你应该将编译好的包文件放在一个类似这样的文件夹中:

myworkspace/pkg/linux_amd64/path/to/parent/package/abc.a

或者在Windows 64位的情况下:

myworkspace/pkg/windows_amd64/path/to/parent/package/abc.a

这个页面提供了更多详细信息:如何编写Go代码

注意:

通过这样做,你将限制编译应用程序的可能目标,因为编译好的包对象只适用于特定的平台。在Go中,分发包的首选方式是以源代码形式进行。

英文:

You put your compiled package into the wrong folder.

The pkg folder of your Go workspace contains compiled package objects organized into subfolders based on the target architecture they are compiled to, for example linux_amd64 or windows_amd64. Inside that the path of the parent package follows, and the direct parent folder is the place to put the compiled package object into.

Check where go install puts your package object, and mirror that folder structure.

So for example if your package is path/to/parent/package/abc and you compile to linux 64-bit, you should put its compiled package file into a folder like:

myworkspace/pkg/linux_amd64/path/to/parent/package/abc.a

or in case of Windows 64-bit:

myworkspace/pkg/windows_amd64/path/to/parent/package/abc.a

This page gives you more details: How to Write Go Code

Notes:

By doing this you will limit the possible targets to compile your app to, as a compiled package object is only for a specific platform. The preferred way to distribute your packages in Go is in source code form.

huangapple
  • 本文由 发表于 2015年5月26日 18:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/30455537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定