Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

build - Can I have a library and binary with the same name?

I'm building a library, but I also want it to be usable as a standalone binary.

For example, let's say I'm building an implementation of Tar. Tar is commonly used as a command, but it can also be used as a library. Intuitively, I'd do something like this:

src/
    tar/
        tar.go # belongs to package tar
        main.go # imports tar and provides a main function

This doesn't seem to work though. From the documentation, it seems that "commands" should have a separate name from the library. Adapting the example given there to this example, I have the following directory structure:

src/
    tar/
        tar.go # belongs to package tar
    tarbin/
        main.go # imports tar and provides a main function

However, this creates a command called tarbin, not tar in $GOPATH/bin. The workaround I've found is to do go build -o $GOPATH/bin/tar tar, but I get the feeling I'm doing something horribly wrong. Is there a better way?

Note: I'm well aware that tar is included in the standard libs, I'm only using it as an example.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...