我可以使用相同的名称来命名库和二进制文件吗?

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

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

问题

我正在构建一个库,但我也希望它可以作为一个独立的二进制文件使用。

例如,假设我正在构建一个Tar的实现。Tar通常用作一个命令,但它也可以作为一个库使用。直观地,我会这样做:

src/
    tar/
        tar.go # 属于tar包
        main.go # 导入tar并提供一个main函数

然而,这似乎不起作用。根据文档所述,"命令"应该与库有一个单独的名称。将那里给出的示例调整到这个示例中,我有以下目录结构:

src/
    tar/
        tar.go # 属于tar包
    tarbin/
        main.go # 导入tar并提供一个main函数

然而,这将在$GOPATH/bin中创建一个名为tarbin而不是tar的命令。我找到的解决方法是执行go build -o $GOPATH/bin/tar tar,但我感觉我做错了什么。有更好的方法吗?

**注意:**我很清楚tar已经包含在标准库中,我只是用它作为一个例子。

英文:

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.

答案1

得分: 21

我可能会这样做

src/
    tar/
        tar.go         # tar库
        tar/
            main.go    # tar可执行文件

这将给你一个名为tar的可执行文件和一个名为tar的库

假设你将其托管在github上,那么你可能想要这样

src/
    github.com/
        you/
            tar/
                tar.go         # tar库
                tar/
                    main.go    # tar可执行文件

这将在你执行go get install github.com/you/tar/tar时给你一个名为tar的可执行文件,并在你执行go get install github.com/you/tar时给你一个名为github.com/you/tar的库

根据你认为哪个更重要,你可以交换库和可执行文件的位置

src/
    github.com/
        you/
            tar/
                main.go            # tar可执行文件
                tar/
                    tar.go         # tar库

将所有代码放在一个树中使你能够从根目录执行go install ./...来构建所有的包和子包,这是一个优势。go test|fmt ./...也是如此。(请注意,这里真的是三个点!)

英文:

I'd probably do this

src/
    tar/
        tar.go         # tar libary
        tar/
            main.go    # tar binary

That will give you a binary called tar and a library called tar

Let's say you are hosting this on github then you'd want

src/
    github.com/
        you/
            tar/
                tar.go         # tar libary
                tar/
                    main.go    # tar binary

Which would give you a binary called tar when you do go get install github.com/you/tar/tar and a library called github.com/you/tar when you do go get install github.com/you/tar

Depending on which you feel is more important you could swap the library and the binary over

src/
    github.com/
        you/
            tar/
                main.go            # tar binary
                tar/
                    tar.go         # tar libary

Keeping all the code in one tree enables you to do go install ./... from the root to build all packages and subpackages which is an advantage. go test|fmt ./... also. (Note that really is 3 dots!)

huangapple
  • 本文由 发表于 2013年1月12日 02:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/14284375.html
匿名

发表评论

匿名网友

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

确定