<p>What are the popular web frameworks in golang right now? The ones I've discovered on my own are:</p>
<p>Gin
Martini
Gorilla Toolkit
Macaron
Beego</p>
<p>I haven't fallen in love with any yet, although Macaron and Martini look OK. </p>
<p>Also, what about templating? Pongo2 and html/template are my options?</p>
<hr/>**评论:**<br/><br/>sageProgramming: <pre><p>Honestly, most Gophers use the standard library for things other languages need external frameworks and libraries for. text/template for example and it's html counterpart work great right out of the box. I know several companies that just use net/http. Go's simple syntax and interfaces make external libraries overkill in a lot of areas where other languages require a custom dsl to solve the same problem.</p></pre>defcube: <pre><p>Thanks. Middleware that can write to a context seems to be the biggest improvement that could be made over just vanilla go. For example, take the cause of auth middleware. If you authenticate a user, it'd be nice to stick the currently authenticated username into a variable somewhere and maybe even lazy-load the rest of the info about the user on-demand. </p>
<p>Edit: I could use github.com/gorilla/context to solve this.</p></pre>-Nii-: <pre><p>Have a look at xHandler and the related blog post from Dailymotion which does what you need.</p></pre>schumacherfm: <pre><p>+1 for xhandler -1 that xhandler abbreviates Context with C <a href="https://github.com/rs/xhandler/blob/master/xhandler.go#L21" rel="nofollow">https://github.com/rs/xhandler/blob/master/xhandler.go#L21</a> ;-)</p>
<p>I don't use gorilla/context because of the underlying global map. Stick to context.Context</p></pre>defcube: <pre><p>The global map scared me too. I didn't read all of gorilla.context's code, but it seems to me we'd want a safety goroutine to expire stale contexts from the global map, just in-case one didn't get cleared for some reason.</p>
<p>I'll check out xhandler. Thanks</p></pre>dhdfdh: <pre><p>Redditors are always looking for shortcuts to work around learning how to program. You should probably stick to PHP.</p></pre>defcube: <pre><p>Quite insulting, and I haven't done php in 10 years. I also have a CS degree and have programmed in many languages. The answer telling me what most gophers do was perfect and assumed I'm ignorant about the go programming community, not programming in general. </p></pre>idiocrates: <pre><p>Don't sweat this asshat's responses. He pretends that he is some great programmer, but has never answered one question with an actual response. Just look at his history. It's full of insults and zero substance. He just needs to make himself feel better about his inferior abilities.</p></pre>MrNaviPacho: <pre><p>Agreed, you owe us no explanation; your questions are spot on.</p></pre>MrNaviPacho: <pre><p>Redditors are always looking for someone to insult without even answering their question. You should probably stick to the other comments. Edit: In all seriousness, does anyone know if the community finished that code of conduct? I never understood why it was needed, until now.</p></pre>idiocrates: <pre><p>Go look at his history. He has a habit of insulting people going on about how he's a great programmer while never providing substance, <a href="https://www.reddit.com/r/golang/comments/3rysdf/cant_import_http2_package/">yet he was unable to import a package here</a>. There are several other examples where he's asking simplistic questions and greeted with civil and accommodating answers.</p>
<p><a href="https://www.reddit.com/r/golang/comments/3hgwmd/shared_variable_in_package/">https://www.reddit.com/r/golang/comments/3hgwmd/shared_variable_in_package/</a></p>
<p><a href="https://www.reddit.com/r/golang/comments/3s299f/templateparsefiles_from_directory/">https://www.reddit.com/r/golang/comments/3s299f/templateparsefiles_from_directory/</a></p>
<p>Just look at his history. Don't be discouraged by this absolute cunt. Asking questions is how we learn.</p></pre>dhdfdh: <pre><p>True but gutter questions should be met with such answers and, while you may not like what I said, it's really no different than what others are saying. Just more direct instead of:</p>
<blockquote>
<p>most Gophers use the standard library for things other languages need external frameworks and libraries for.</p>
</blockquote></pre>MrNaviPacho: <pre><p>Your comment is nothing at all like the one you quoted. All you do is imply that he doesn't know how to program and doesn't even want to learn. This is rather insulting (and probably wrong) as he's likely asking the question because he wants to learn. So, unless you're saying that "You should probably stick to PHP." was your genuine, no irony suggestion, you should probably apologize for your backhanded reply, and if you can, try to answer his question.</p></pre>defcube: <pre><p>Perfect rebuttal, thank you! </p></pre>dwieeb: <pre><p>Stick with the standard library for templating, at least. Block templating is coming in Go 1.6. </p>
<p>Other people here may insist you use the standard library for <em>everything</em>, and indeed you should know it well so you can make informed decisions on when you really need additional dependencies in your code. Personally, I don't have time to program a database driver, for example, so hell yes I'm going to use <code>lib/pq</code>.</p>
<p>We tend to look for things like "routing" because we come from MVC frameworks. So we look for ways to bring familiar tools into unfamiliar environments.</p>
<p>Perhaps the best advice is give it a go with the standard library. If you find your code base is growing out of it and requires structure that you were unable to give it, you can then invest the time it takes to thoroughly research and test these frameworks to find the one for you.</p></pre>ergotayours: <pre><p>I think that when people say "stick with the standard library", they mean "use the packages in the standard library directly instead of wrapping them in a framework". I don't think they mean to suggest you can't use any third-party packages, because, like you say, a lot of them implement functionality that doesn't exist in the standard library.</p></pre>madtopo: <pre><p>Here you can find a compiled list of web frameworks for Go, along with other tool <a href="https://github.com/avelino/awesome-go">https://github.com/avelino/awesome-go</a></p></pre>Gouthamve: <pre><p>Checkout echo github.com/labstack/echo. Its fast and has a nice api. But its not compatible with the standard handlers</p></pre>3Dayo: <pre><p>I'd suggest that you start with echo, then as you get familiar with go (both the language and the standard lib) you can work your way back to just using the standard lib (if required).</p></pre>defcube: <pre><p>Nice simple framework. Why use this over something like gocraft/web?</p></pre>foxh8er: <pre><p>I agree. Benches well, and the API is super simple for what I need (and I need it to be sinatra-esque)</p></pre>dhdfdh: <pre><blockquote>
<p>its not compatible with the standard handlers</p>
</blockquote>
<p>Then that's a reason <strong>not</strong> to check it out.</p></pre>Gouthamve: <pre><p>I disagree, I use it in production and love it. Its fast and robust and has a very easy API. While its not compatible directly with http.Handler, its easy to wrap the standard handler like this:</p>
<pre><code>func echoHandler(c *echo.Context) error {
response := c.Response()
w := response.Writer()
req := c.Request()
httpHandler(w, req)
return nil
}
</code></pre></pre>BoTuLoX: <pre><p>I use gin merely because it wraps around the standard library and httprouter.</p></pre>bkeroack: <pre><p>"Frameworks" are not idiomatic in Go. Use libraries. 90% of the time the standard library (net/http in this case) is sufficient.</p></pre>elithrar_: <pre><p>This certainly depends on your definition of 'framework'. Whilst I'd (personally) recommend avoiding large, kitchen-sink frameworks - which don't mesh well with the Go ecosystem - smaller, focused frameworks can be useful. Most of the latter are just router+context+middleware libraries with some useful glue, but that's glue you don't have to write yourself, with some advantages due to the assumptions the framework author can make as a result.</p>
<p>net/http on its own (no pattern matching, no contexts, no middleware libs) is the foundation for any Go web service, but not the only tool to use.</p></pre>blueblank: <pre><p>By no means popular, but my favorites because I built them:</p>
<p><a href="https://github.com/thrisp/flotilla" rel="nofollow">Flotilla</a> -- flask like library on top of net/http</p>
<p><a href="https://github.com/thrisp/djinn" rel="nofollow">Djinn</a> -- jinja like templating on top of html/template</p></pre>gernest_: <pre><p>Try <a href="https://github.com/gernest/utron" rel="nofollow">https://github.com/gernest/utron</a></p></pre>skarlso: <pre><p>Why -2 points? The guy put a lot of effort into that framework. The least thing to do would be a no thanks, and move along. Or if you really have to, then at least explain your down vote. Let's not make reddit into stackexchange people. </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传