Download - Metaprogramming Go
About Me
• Weng Wei, Chinese in Singapore
• Full-stack developer
• .net -> python -> go
• Passionate about code & life
Metaprogramming (MP)
• “writing of computer programs with the ability to treat programs as their data...program could be designed to read, generate, analyse and/or transform other programs”
• allows programmers to minimize the number of lines of code to express a solution => better produtivity
MP is everywhere
• Compiler is the most common MP tool, it compile high level code to lower one
• MP for manipulating other code is also common, for example: C Macro
Metaprogramming Go?• No macro
• No generic
• Reflection in Go is awful
• “ gopher has been hard working these years… to solve problems don’t exist in other languages……” - @laiyonghao
auto generate• MakeFile?
• Real-time file monitor: github.com/gorakhargosh/watchdog
Struct Parsing
• ptsd: github.com/wickman/ptsd
• thrift lexer/parser using ply
• ply: www.dabeaz.com/ply/
• An implementation of lex and yacc parsing tools for Python
API• Needed in different places:
• web
• mobile web
• mobile client
• Business are the same, but may need different wrapping
• Above are “demo codes”
• Actual code are much more complicated
• ORM:github.com/sipin/xuanwu
• Just for referencing of idea; don’t use it
• No documentation
• No maintain
• We’ve restarted
Customisation?
• Generate once, and change
• Generate whenever template changes:
• Provide callback interface
• Method overwriting
• May use MP for generic, ORM, workflow, API wrapping, and more
• Consider MP as code-refactoring
• Write the dirty code first
• Abstract similar code repeated to template
• Choose an DSL
• Python is simple & resourceful; handy for metaprogramming prototype/PoC
Challenge of MP• Choosing the right DSL
• Provide both:
• high level of abstraction
• low level customization
• www.joelonsoftware.com/articles/LeakyAbstractions.html
Code generation
• Parse (DSL) & Bind (code-template) are easy
• just like HTML template
• Generated codes are simple, thus easy to debug
• Compiler checking for free
Reflection for LISP way?
• No compilation
• Error checking at runtime
• Implementation needs more challenger
• Codes are more elegant
• Yup, that could be better; so, how?
Actually• Python is not so fast
• After experiencing git’s extreme speed, you can’t tolerate hg’s “not so fast”
• It’s easier to use & analyse the same language in MP
• golang.org/pkg/go/parser/
• go generate in go 1.4
• docs.google.com/document/d/1V03LUfjSADDooDMhe-_K59EgpTEm3V8uvQRuNMAEnjg
• Metaprogramming Go with GO is the way
• with py only for quick prototype/POC