What is the best way to manage and include multiple files using Go?

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

What is the best way to manage and include multiple files using Go?

问题

我正在尝试使用Go和Google AppEngine,并且希望在主文件中导入或包含多个文件。

例如,假设我有一个包含多个网址函数的init文件,如下所示:

func init() {
    http.HandleFunc("/", root)
    http.HandleFunc("/1", function1)
    http.HandleFunc("/two", function2)
    http.HandleFunc("/3hree", function3)
}

我希望每个函数都在单独的文件中(例如,这样其他开发人员可以独立地处理它们,或者在调试时更容易阅读)。

我不想为每个函数创建单独的包,因为那似乎有点过头了,但我认为这可能是唯一的方法。

另一种提问方式:

如果你想要一个只包含模板的单独文件,最好的方法是将其引入到Go文件中(而不必内联):

const testPage = `
<!DOCTYPE html>
<html>
<body>
    <br>this is a test
</body>
</html>`

以上是翻译好的内容,请确认是否满意。

英文:

I am experimenting with Go and the Google AppEngine, and I would like to have multiple files imported or included within a main file.

For example, suppose I have a file with an init that has multiple web url functions, such as:

func init() {
	http.HandleFunc(&quot;/&quot;, root)
	http.HandleFunc(&quot;/1&quot;, function1)
	http.HandleFunc(&quot;/two&quot;, function2)
	http.HandleFunc(&quot;/3hree&quot;, function3)
}

I would like to have each function be in a separate file (for example, so other developers can work on them independently -- or just for ease of readability when debugging).

I don't want to make separate packages for each function, because that seems like overkill, but I think that might be the only way to do this.

Another way of asking the same question:

If you wanted a separate file with just a template, what is the best way to have this be pulled into a go file (without having to have it be in-line):

const testPage =
`&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
	&lt;br&gt;this is a test
&lt;/body&gt;
&lt;/html&gt;`

答案1

得分: 2

你的应用程序肯定可以由多个文件(和多个“包”)组成。每个文件都可以有自己的init()函数,因此你可以在每个文件中注册处理程序,或者将它们集中在单个文件中。

例如:

root.go:

package app
…
func init() {
  http.HandleFunc("/root", rootHandler)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
   …
}

other.go:

package app
…
func init() {
   http.HandleFunc("/1", oneHandler)
}

…等等。

最好将模板HTML放在与Go文件相邻的文件中,而不是嵌入在字符串中(你也可以将其存储在数据存储中,但这需要更多的工作)。看看SDK中的演示示例-曼德勃罗集的示例使用了map.html文件。

英文:

Your application can certainly be composed of multiple files (and multiple packages). Each of those files can have their own init() functions too, so you can register handlers in each file – or keep them central in a single file.

For example:

root.go:

package app
…
func init() {
  http.HandleFunc(&quot;/root&quot;, rootHandler)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
   …
}

other.go:

package app
…
func init() {
   http.HandleFunc(&quot;/1&quot;, oneHandler)
}

… etc.

It's best if you put your template HTML in its own files alongside your Go files, rather than embedding it within strings (you could store it in the datastore too, but that's a little more work). See how the demos in the SDK do it – the mandelbrot one does this with its map.html file.

答案2

得分: 1

在Google App Engine上,由于无法访问Go的文件系统模块,这是不可能的。你无法将文件作为文件系统进行访问。相信我,我已经尝试过了 What is the best way to manage and include multiple files using Go?

像你提到的那样,最好的选择是内联。理论上,你可以通过将每个模板作为一个包来实现一些分离。但本质上是相同的概念。

另一个选项是一种巧妙的方法,将模板存储在GAE数据存储中。但这样你就需要支付数据存储的IO操作费用。而且即使这样也是一种内联的方式,本质上是一样的。

英文:

On Google App Engine this isn't possible since you do not have access to the file system module for Go. You can't access files as a file system. Trust me. I've tried. What is the best way to manage and include multiple files using Go?

The best option like you have mentioned is inline. You could in theory have some separation by having each template as a package. But it is the same concept.

Another option which is kind of a hack is to store your templates in the GAE data store. But then you're paying for data store IO operations. And even then it's a hack which is the same as inlining.

huangapple
  • 本文由 发表于 2013年10月16日 13:05:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/19395625.html
匿名

发表评论

匿名网友

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

确定