Why use Go for web development?

Post on 15-Jul-2015

742 views 1 download

Transcript of Why use Go for web development?

Why use Go for Web development?

Weng Wei, July 2014

About Go

–Rob Pike

“Go is a programming language designed by Google to help solve Google's (big)problems. ”

–Derek Collison

“ Go will become the dominant language for systems work in IaaS, Orchestration, and PaaS in 24 months.”

Go looks cool!but, it is suitable for simple web development?

Web? Use MVC!

• Quite a number of Go MVC web framework exist:

• Beego / Martini / Revel….

• Different approach for MC layers

• V layer are mostly the same: Go HTML Template http://golang.org/pkg/html/template/

@yinwang0:Go HTML Template• “Basically usable” template engine

• Surprisingly, logic control code in template are not Go itself

• Poor expressiveness

• Need to bind all template data into a single model

• Helper func need to be “registerd”

• Quite messy & tedious

–Wang Yin

“It’s better to build website with more mature frameworks from other language”

Template engine must be good for web development!

– Wikipedia

“PHP(PHP: Hypertext Preprocessor)…server-side scripting language designed for web development.. PHP code can be

simply mixed with HTML code..”

Mix html with Go?

• “Not possible, Go doesn’t have interpreter”

• “Go is NOT script”

I think otherwise• Go is turing complete

• No things Go can’t go

• Only thing more complicated to do with Go

• Embedding static compile language in HTML are possible:

• ASP.Net

Razor

• Default template/view engine for ASP.NET MVC

• Support embedding c# / vb.net in HTML

• .Net developers love it (including me):

• http://www.zhihu.com/question/19973649 (Chinese link)

How complicated to create GoRazor?

GoRazor

• http://github.com/sipin/gorazor

• PoC in one night

• Continuous team improvement:214 commits

• Proven with internal large web project

• Mature template engine; sublime plug-ins for syntax highlight + auto complete

• Able use Go directly in template; no different syntax for flow control

• Powerful expressiveness

• Rendering template is calling function; models are function parameters

• No need to register helper func, directly import in template

• Very concise & convinient

Demo

• If you think Go is not suitable for web development because of poor template engine

• Please try https://github.com/sipin/gorazor

Of cause, template engine is only the start

Is Go suitable for dynamic web development?

Dynamic• web is built around string

• Go is static compiled; can it:

• Obtain object properties via string?

• Invoke package/object methods via string?

• Create object via string?

Reflection

• Reflection could meet most of the requirements

• Check Martini

Issues with Go’s reflection

• Let’s put performance aside

• Tedious & complicated

• Move compilation error to runtime error: no complier check

Reflection is not the only way

Code generation approach

For example

• Bind HTTP Post form values to object

• Generate all these methods:

• NewXXXObjByID(namespace, classname, id string)

• ReadForm(params map[string]string)

• So, 600k LoC

• Code generation makes static compiled language feels “dynamic”

• Go can do this, so does all other languages

• But, how about the compilation speed?

Full Compliation

Incremental compilation after editing template

• Go’s compilation speed is amazing!

• This is a project with 600k LoC on Mid 2011 Macbook Air

• We could keep cmd + s / cmd + tab / cmd + R development pace

Back to dynamic

• If complication is slow, then code-gen approach hurts produtivity

• Go compile extreme fast

• Go has unique point for dynamic

– Rob Pike

“interfaces.. the true Go philosophy…”

Go’s interface

• Define class/method first

• May define interface later

• Interface query at run-time

Example

• Check if object has “GetUserID” method

• If so, check if current user

• If so, allow edit

More example

• Check if object has “IsDistributed”、“GetPersistentNode” methods

• If so, replicate object to other nodes

Combine the examples

• Go allow querying original object’s interface

• Switch between different interfaces at runtime

• Interface could be defined in new packages

• No need to put all interface combinations for different purpose at one place

However, why reinventing the web MVC wheel?

But!• Simple web pages? Pick whatever you like

• Of cause including Go

• Middle size web application, like a forum?

• php / Rails / Django / asp.net are more mature

• Go also can, if you like re-inventing the wheel

• How about large website?

Needs for large website• Stability rules

• Scalability & Distributed are important

• SOA architecture

• Performance are critical

• Maintainable code (With GoRazor; even the templates are strong-typed & compiler checked)

• Quick prototyping, and switch technology/platform?

• Works, but these are technical debt!

• If we could owe less debt since the beginning, why not?

Go is suitable to development large website!

Finally, I would like to talk about Go & startup

Q & A

Reference• https://talks.golang.org/2012/splash.slide

• http://open.qiniudn.com/go-next-c-pptx.pdf

• http://www.csdn.net/article/2012-09-14/2809984-will-go-be-the-new-go-to-programming-lan

• http://weibo.com/1889019865/B4nx3z6GF

• http://www.yinwang.org/blog-cn/2014/04/18/golang/

• http://zh.wikipedia.org/wiki/PHP

• http://www.ituring.com.cn/article/1339