5

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.