Go Makefile 混乱

发布于 2024-12-08 20:09:30 字数 374 浏览 0 评论 0原文

我正在编写一个 Go 包,我们称其为“coolstuff”。我想包含一个 example.go 文件,该文件运行来自 Coolstuff API 的示例。但是默认的Makefile不会构建coolstuff包和example.go,抱怨:

example.go:1: package main; expected coolstuff
example.go:3: can't find import: coolstuff

我如何a)编译包,b)安装包,以便我将来编写的任何代码都知道如何找到coolstuff,以及c)构建example.go,它依赖于coolstuff?

我是否需要两个 Makefile,一个用于包,一个用于 example.go?

I'm writing a Go package, let's call it coolstuff. I want to include an example.go file that runs examples from the coolstuff API. But the default Makefile won't build the coolstuff package and example.go, complaining:

example.go:1: package main; expected coolstuff
example.go:3: can't find import: coolstuff

How can I a) compile the package, b) install the package so that any future code I write will know how to find coolstuff, and c) build example.go, which depends on coolstuff?

Do I need two Makefiles, one for the package and one for example.go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-12-15 20:09:30

有几种方法可以做到这一点。

1)这并不完全是您所描述的,但可以将一个名为 example_test.go 的文件添加到您的 Coolstuff 包中。如果你只是去安装你的包,这个文件将被忽略,但是当你在你的coolstuff中执行 gotest 时package,然后 gotest 将自动构建包含所有 *_test.go 文件的包并运行它们。在我看来,这是最简单的方法,因为您不必处理多个包,并且还可以提供许多独立的示例/测试用例。未来的 go 版本将能够将这些示例添加到 godoc pkg 文档中。

2) 如果您对使用 Makefile 感到不舒服,那么您可能需要仔细查看 goinstall。您可以设置 GOPATH 环境变量来定义您自己的项目目录。页面底部给出了适合 goinstall 的示例目录结构。 (“bar”与您的“coolstuff pkg”类似,“qux”可能是您的“示例”)。goinstall 会自动为您找出所有这些构建依赖项。3

)也可以使用 Makefile。其中两个,一个基于 Make.pkg 作为您的 Coolstuff 包目录,另一个基于 Make.cmd 作为您的示例目录,这听起来是个好主意,您可以找到一些 go Makefiles 的示例。 此处

There are several ways to do that.

1) That's not exactly what you have described, but it is possible to add a file called example_test.go to your coolstuff package. If you just goinstall your package, this file will be ignored, however when you are executing gotest inside your coolstuff package, then gotest will automatically build your package including all *_test.go files and run them. That`s in my opinion the easiest way, since you do not have to deal with several packages and you can also provide many independent examples / test cases. Future go versions will be able to add those examples to your godoc pkg documentation.

2) If you feel uncomfortable about using Makefiles, then you might want to take a closer look at goinstall. You can set the GOPATH environment variable to define your own project directory. An example directory structure which is suitable for goinstall is given at the bottom of the page. ("bar" is similar to your "coolstuff pkg there, and "qux" might be your "example"). goinstall will figure out all those build dependencies automatically for you.

3) Using Makefiles is also possible. Creating two of them, one which is based on Make.pkg for your coolstuff package directory and another one which is based on Make.cmd for your example directory sounds like a good idea. You can find some examples of go Makefiles here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文