Go Makefile 混淆

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

Go Makefile confusion

问题

我正在编写一个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) 构建依赖于coolstuff的example.go?

我需要两个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?

答案1

得分: 3

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

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

2)如果你对使用Makefiles感到不舒服,那么你可能想更仔细地了解一下goinstall。你可以设置GOPATH环境变量来定义你自己的项目目录。适用于goinstall的示例目录结构在页面底部给出。("bar"类似于你的"coolstuff pkg","qux"可能是你的"example")。goinstall将自动为你解决所有这些构建依赖关系。

3)使用Makefiles也是可能的。创建两个Makefiles,一个基于Make.pkg用于你的coolstuff包目录,另一个基于Make.cmd用于你的example目录,听起来是个好主意。你可以在这里找到一些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.

huangapple
  • 本文由 发表于 2011年10月10日 07:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/7707309.html
匿名

发表评论

匿名网友

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

确定