why use go for web development?

57
Why use Go for Web development? Weng Wei, July 2014

Upload: weng-wei

Post on 15-Jul-2015

742 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Why use Go for web development?

Why use Go for Web development?

Weng Wei, July 2014

Page 2: Why use Go for web development?

About Go

Page 3: Why use Go for web development?

–Rob Pike

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

Page 4: Why use Go for web development?

–Derek Collison

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

Page 5: Why use Go for web development?

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

Page 6: Why use Go for 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/

Page 7: Why use Go for web development?

@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

Page 8: Why use Go for web development?

–Wang Yin

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

Page 9: Why use Go for web development?

Template engine must be good for web development!

Page 10: Why use Go 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..”

Page 11: Why use Go for web development?

Mix html with Go?

Page 12: Why use Go for web development?

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

• “Go is NOT script”

Page 13: Why use Go for web development?

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

Page 14: Why use Go for web development?
Page 15: Why use Go for web development?

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)

Page 16: Why use Go for web development?

How complicated to create GoRazor?

Page 17: Why use Go for web development?

GoRazor

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

• PoC in one night

• Continuous team improvement:214 commits

• Proven with internal large web project

Page 18: Why use Go for web development?
Page 19: Why use Go for web development?

• 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

Page 20: Why use Go for web development?

Demo

Page 21: Why use Go for web development?

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

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

Page 22: Why use Go for web development?

Of cause, template engine is only the start

Page 23: Why use Go for web development?

Is Go suitable for dynamic web development?

Page 24: Why use Go for 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?

Page 25: Why use Go for web development?

Reflection

• Reflection could meet most of the requirements

• Check Martini

Page 26: Why use Go for web development?

Issues with Go’s reflection

• Let’s put performance aside

• Tedious & complicated

• Move compilation error to runtime error: no complier check

Page 27: Why use Go for web development?

Reflection is not the only way

Page 28: Why use Go for web development?

Code generation approach

Page 29: Why use Go for web development?

For example

• Bind HTTP Post form values to object

Page 30: Why use Go for web development?
Page 31: Why use Go for web development?
Page 32: Why use Go for web development?
Page 33: Why use Go for web development?
Page 34: Why use Go for web development?

• Generate all these methods:

• NewXXXObjByID(namespace, classname, id string)

• ReadForm(params map[string]string)

• So, 600k LoC

Page 35: Why use Go for web development?

• Code generation makes static compiled language feels “dynamic”

• Go can do this, so does all other languages

• But, how about the compilation speed?

Page 36: Why use Go for web development?

Full Compliation

Page 37: Why use Go for web development?

Incremental compilation after editing template

Page 38: Why use Go for web development?

• 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

Page 39: Why use Go for web development?

Back to dynamic

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

• Go compile extreme fast

• Go has unique point for dynamic

Page 40: Why use Go for web development?

– Rob Pike

“interfaces.. the true Go philosophy…”

Page 41: Why use Go for web development?

Go’s interface

• Define class/method first

• May define interface later

• Interface query at run-time

Page 42: Why use Go for web development?

Example

• Check if object has “GetUserID” method

• If so, check if current user

• If so, allow edit

Page 43: Why use Go for web development?
Page 44: Why use Go for web development?

More example

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

• If so, replicate object to other nodes

Page 45: Why use Go for web development?
Page 46: Why use Go for web development?

Combine the examples

Page 47: Why use Go for web development?
Page 48: Why use Go for web development?
Page 49: Why use Go for web development?

• 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

Page 50: Why use Go for web development?

However, why reinventing the web MVC wheel?

Page 51: Why use Go for web development?

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?

Page 52: Why use Go for web development?

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)

Page 53: Why use Go for web development?

• Quick prototyping, and switch technology/platform?

• Works, but these are technical debt!

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

Page 54: Why use Go for web development?

Go is suitable to development large website!

Page 55: Why use Go for web development?

Finally, I would like to talk about Go & startup

Page 56: Why use Go for web development?

Q & A

Page 57: Why use Go for web development?

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