英文:
Why can't I add a main to my library in golang?
问题
我正在努力完成一个本应该很简单的任务。我了解GitHub的代码组织模型(即一个库仓库和一个使用该库的应用程序仓库)。我认为这个模型非常棒。但是我经常发现我希望mylib
与一个简单的可执行文件捆绑在一起,放在一个单独的main.go
文件中。main.go
应该是package main
,并且应该导入mylib
。换句话说,它应该是构建使用该库的应用程序的准确文档。
我的观点是,由于提供一个简单的命令行界面来包装你的库通常足够方便,所以应该有一种简单的方法来做到这一点,而不必创建另一个仓库,而且Go语言应该提供帮助。
我希望有类似下面这样的结构:
$GOPATH/src/github.com/me/mylib
mylib.go
mylib_also.go
main.go
其中mylib
是库(package mylib
),main.go
是package main
,运行go install
命令后会生成bin/mylib
和pkg/mylib.a
。
要么main.go
应该导入"github.com/me/mylib"
(如果我现在这样做,会得到循环导入错误),要么go
应该能够理解发生了什么,因为这个功能应该是内置的,仓库中的main.go
生成了可执行文件。可能要求导入(并消除循环错误)是更好的方式。
目前,我必须这样做:
$GOPATH/src/github.com/me/mylib
mylib/
mylib.go
main.go
所以我必须导入github.com/me/mylib/mylib
,这太荒谬了。
总之,go install
应该允许包和主程序的特殊情况,主程序导入该包并提供一个简单的命令行界面,以演示该包的API。GitHub模型推崇两个仓库,但是在一个仓库中做一个简单的命令行界面应该很容易!
英文:
I'm having trouble achieving what should be an easy task. I understand the GitHub model for code organization (ie a library repo and an app repo that consumes the library). I think it's fantastic. But I find often that I want mylib
to come bundled with a simple executable in a single main.go
file. The main.go
should be package main
and should import mylib
. In other words, it should be an exact documentation of how to build an app that consumes this library.
My point is, since it is often enough convenient to provide a simple command line interface that wraps your library, there should be an easy way to do this without having to make another repo, and golang should help.
I'd like something like the following:
$GOPATH/src/github.com/me/mylib
mylib.go
mylib_also.go
main.go
where mylib
is the library (package mylib
) and main.go
is package main
and on running go install
it generates bin/mylib
and pkg/mylib.a
.
Either main.go
should import "github.com/me/mylib"
(if I do that now, I get cyclical import error) or go
would understand what's happening since this feature should be built in and the one main.go
in the repo generates the exec. Probably requiring the import (and dropping the cyclical error) is the better way.
Right now, I have to do
$GOPATH/src/github.com/me/mylib
mylib/
mylib.go
main.go
So I have to import github.com/me/mylib/mylib which is ridiculous.
In sum, go install
should allow the special case of a package and a main which imports the package and provides a simple cli that demonstrates the packages API. The GitHub model promotes two repos, but it should be easy to do simple clis in one!
答案1
得分: 9
你不能在同一个文件夹中拥有多个包。Go是基于包级别而不是文件级别进行操作的。
在这种情况下,惯例是创建一个cmd
文件夹,其中包含你的包的主要文件 - 例如,参考https://github.com/bradfitz/camlistore/tree/master/cmd或https://github.com/boltdb/bolt。
在你的情况下,应该是这样的:
$GOPATH/src/github.com/me/mylib
mylib/
mylib.go
README.md
cmd/
your-lib-tool/
main.go
英文:
You can't have multiple packages per folder. Go operates on a package-level, not a file level.
Convention in this case—a binary that consumes your library—is to create a cmd
folder with your package main - i.e. as per https://github.com/bradfitz/camlistore/tree/master/cmd or https://github.com/boltdb/bolt
In your case this would be:
$GOPATH/src/github.com/me/mylib
mylib/
mylib.go
README.md
cmd/
your-lib-tool/
main.go
答案2
得分: 2
如果你只是想要一个方便的、简单的封装,只是为了演示目的而需要一个命令行接口来调用你的库,而且如果主程序不是通过go get
构建的(也不是通过简单的go build
和go install
),而是通过go run main.go
或手动构建(例如使用6g
)也可以接受的话,你可以在main.go中使用一个构建标签(参见http://golang.org/pkg/go/build/#hdr-Build_Constraints):
// +build ignore
这样你就不需要一个不同的文件夹或仓库了。
英文:
If you just want a convenient, little wrapper, just some command line interface to your lib for demonstration purpose and it is okay if the main program is not built on go get
(and not during simple go build
and go install
) but you are okay by running it via go run main.go
or building it manually e.g. with 6g
: Just use a build tag (see http://golang.org/pkg/go/build/#hdr-Build_Constraints) in main.go:
// +build ignore
and you won't need a different folder or repo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论