英文:
Go: Embedded backend vs app engine
问题
我是那种经典的本地程序员之一,大部分时间都在处理.exe和.jar文件。过去一年,我投身于Web框架和技术的世界,这些技术让我印象深刻。过去一个半月,我爱上了Go,因为它的严谨性,以及它看起来是“独立”的。现在是真正的问题...
-
Go应用引擎应用程序,为什么我们需要它?
-
选择封装应用程序(框架)的差异和理由是什么?
我猜它的目的是将一些通信任务从应用程序转移到封装器上,但可惜的是,我无法通过文档和讨论找出这种模块化背后的具体目的。
最好的问候和向你致以虚拟高五!
英文:
I'm one of those classic native programmers that has spent most of his past with .exe's and .jar's. As of the past year I've thrown my self into the world of web frameworks and technologies that seize to impress me. As of the past 1½ month I have fallen In love with Go because of it's strictness, and also how 'stand-alone' it seems to be. So now to the real question...
-
Go app engine application, why do we need this?
-
What is the difference and reasoning to choose a wrapped application (framework)?
I assume its purpose is to load some of the communication off the application to the wrapper, but sadly I can't seem to figure out (through documentation and discussion) what the specific purpose behind this modulation is.
Best regards and cyber high fives in your direction!
答案1
得分: 4
这确实是两个不同的问题。
1. 为什么选择GAE?
这取决于你。GAE提供基于云的托管服务,你需要支付租金来使用。它有点类似于亚马逊网络服务。你的Go应用将被上传到GAE,它将提供你的网络服务,你的用户可以做很多很棒的事情。同时,你永远不需要知道哪个实际的服务器在任何给定的时间提供服务 - 应用可以在它们的服务器之间动态迁移。GAE提供高可用性,并且对于你来说在保持服务器安全、备份等方面的工作量很低。它还可以弹性应对负载增加。
你也可以选择租用一个私有服务器(例如在Rackspace),或者只租用一个虚拟机。你最好需要成为一个Linux专家(在Serverfault上可以得到很多帮助),并且你将不得不自己做备份、防火墙等工作。这可能会花费(很多)更少的费用。或者更多。
2. 选择一个框架?
net/http API允许你编写HTTP服务器代码来实现几乎任何你想要的功能。但是你需要做很多艰苦的工作。相反,像Revel这样的框架使得快速开发服务器成为可能,只要它能满足你的需求。如果你超出了它提供的功能范围,你可能需要做很多研究来找出如何扩展这个框架。
其他有趣的工具包包括Gorilla、Gocraft Web和Goji。就复杂性而言,它们介于Revel和基本net/http之间。
英文:
These really are two different questions.
1. Why GAE?
It's up to you. GAE provides cloud-based hosting that you pay rental to use. It's a bit similar to Amazon Web Services. Your Go app would be uploaded to GAE, where it provides your web service and your users can do lots of wonderful things. Meanwhile you never need to know which actual server is doing the serving at any given time - the app can migrate across their servers dynamically. GAE provides a high uptime and a low effort for you in keeping the server secure, backed up etc. It will also be elastic to cope with surges in load.
You may instead prefer to rent a private server (e.g. at Rackspace) or just a virtual machine. You'd perferably need to be a Linux expert (get lots of help at Serverfault) and you'll have to do the backup, firewall etc all yourself. It may cost (much) less. Or more.
2. Choosing a framework?
The net/http API allows you to write HTTP server code to do pretty well anything you want. But you have to do quite a lot of hard work. At the opposite extreme, frameworks like Revel make rapid server development possible, as long as it does the things you want of it. If you stray into functionality beyond what it offers, you might have to do quite a lot of digging to find out how to extend the framework.
Other interesting toolkits include Gorilla, Gocraft Web and Goji. In terms of complexity, these sit about halfway between Revel and basic net/http.
答案2
得分: 1
回答你的第二个问题,以下是使用框架(例如revel)与使用更简单的工具包(例如gorilla)的一些优缺点。
使用框架的优点包括:
- 提供了许多子包来处理重要的与web相关的子任务,如模板化、生成指定格式的数据(如json或xml)、查询转义等。
- 处理样板式的http处理。
- (希望能够)强制执行最佳实践,如字符串转义。
- 通过强制执行一致的设计模式来帮助您管理复杂性。
使用框架的缺点包括:
- 框架往往是“有主见的”,这意味着您必须接受它们的一般理念并理解它们的核心概念,然后才能使用它们;对于许多框架来说,这可能是相当大的心智负担。
- 额外的抽象层,这意味着您与实际发生的事情又多了一层距离,如果出现问题,将需要更多的理解和调试。
- 它可能是脆弱的,并且很难做一些不是框架中标准用例的事情。
- 未来的可维护性:大多数框架往往没有超长的寿命。Django和Rails已经存在很长时间了,但是在它们之前有很多已经过时的框架。事后诸葛亮,但是很难一开始就选择正确的框架。
建议
很难事先做出决定。很多取决于您问题的具体情况,但是在Go的情况下,我建议选择更简单的选项。其他语言中框架的很多附加值在于它们包含处理重要任务的有用子包,但是Go的标准库已经包含了许多这样的功能(例如encoding/json、net/http、net/url、text/template)。我使用了Gorilla工具包和Go标准库构建了一个相当复杂的Web应用程序,效果非常好,最好的部分是,我可以轻松理解代码的功能,并且可以向他人解释,而不需要他们首先阅读某个第三方框架的大量关于页面。
如果你想了解其他人如何使用Gorilla,可以尝试查看实际使用示例。将其与人们如何使用更复杂的框架进行比较,然后选择你更喜欢的那个。
英文:
To answer your second question, here are some pros and cons of using a framework (e.g., revel) vs. something simpler like a toolkit (e.g., gorilla)
In general, the pros of using a framework are:
- it provides a lot of sub-packages to handle important web-related sub-tasks like templating, generating data in specified formats like json or xml, query escaping, etc.
- it handles boilerplate http handling
- it (hopefully) enforces best practices like escaping strings
- it helps you manage complexity by enforcing a consistent design pattern in the way you handle requests
Cons of using a framework:
- frameworks tend to be "opinionated," meaning you have to buy into their general philosophy and understand their core concepts before you can make use of them; for a lot of frameworks this can be quite a bit of mental overhead
- extra layer of abstraction, meaning you're another step removed from what's really going on, and there will be more stuff to understand and debug if something goes wrong
- it can be brittle and hard to do something that isn't a standard use case in the framework
- future maintainability: most frameworks don't tend to have a super long lifespan. Django and Rails have been around for a long time, but there's a massive graveyard of frameworks that came before them. Hindsight is 20/20, but it's hard to pick the right horse from the outset.
Recommendation
It's hard to make the call upfront. So much depends on the specifics of your problem, but I'd say in the case of Go, opt for the simpler option. Much of the value-add of frameworks in other languages is the fact that they contain useful sub-packages that handle important tasks, but Go already contains a lot of these in its standard library (e.g., encoding/json, net/http, net/url, text/template). I've built a fairly sophisticated web app using just the Gorilla toolkit and the go standard library, and it's been amazingly good, and the best part is, it's incredibly easy to understand what the code does and I can explain it to someone else without requiring them to first read through the massive About page of some third party framework.
If you want to get a sense of how other people use Gorilla, you might try looking at real-world usage examples. Compare that to how people use more sophisticated frameworks and pick whichever you like better.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论