@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
“It’s better to build website with more mature frameworks from other language” –Wang Yin
Template engine must be good for web development!
“PHP(PHP: Hypertext Preprocessor)…server-side scripting language designed for web development.. PHP code can be simply mixed with HTML code..” – Wikipedia
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
• 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
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?
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
“interfaces.. the true Go philosophy…” – Rob Pike
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?