SlideShare a Scribd company logo
Metaprogramming Go
Weng Wei, Nov 2014
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
C Macro
#define list_for_each(pos, head) 
for (pos = (head)->next; pos != (head);
pos = pos->next)
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
How hard working gopher
solve generic with py?
Metaprogramming Go
• StringSet& IntSet has exact code structure, only
types are different
• Classic usage of generic
Metaprogramming Go
Metaprogramming Go
Simple & Effective
auto generate
• MakeFile?
• Real-time file monitor: github.com/gorakhargosh/
watchdog
How about “ORM”?
We need DSL:

thrift
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Same struct definition
• CRUD
• Full-text search
• User Permission
• RPC
Lots of boilerplate code
for obj passing, value
assign, display etc
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
• Generate them all!
• Generated code won’t have typo
Implementation
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
code-gen template
• www.cheetahtemplate.org
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
Metaprogramming Go
YAML is very handy
Define workflow
Metaprogramming Go
Metaprogramming Go
• Similar to ORM: change DSL from thrift to YAML
• YAML
• More flexible
• Non-coder friendly
API
• Needed in different places:
• web
• mobile web
• mobile client
• Business are the same, but may need different
wrapping
Metaprogramming Go
Web Form
JSON HTTP
Mobile API
We should be lazy about
writing boilerplate
wrapping code
Metaprogramming Go
So, we have 300k80line of code,
mostly generated
• 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
Metaprogramming Go
Metaprogramming Go
• 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?
–Ken Thompson
“When in doubt, use brute force.”
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
Q & A
Ad

Recommended

Fast Web Applications with Go
Fast Web Applications with Go
Eylem Ozekin
 
Migrate PHP E-Commerce Site to Go
Migrate PHP E-Commerce Site to Go
Weng Wei
 
Why use Go for web development?
Why use Go for web development?
Weng Wei
 
Functional Programming in PHP
Functional Programming in PHP
Aurimas Niekis
 
Python to go
Python to go
Weng Wei
 
Angular ❤️ CMS from #AngularUp
Angular ❤️ CMS from #AngularUp
Filip Bruun Bech-Larsen
 
RubyConf Taiwan 2016 - Large scale Rails applications
RubyConf Taiwan 2016 - Large scale Rails applications
Florian Dutey
 
Contributing to open source
Contributing to open source
Devin Abbott
 
Making CLI app in ruby
Making CLI app in ruby
Huy Do
 
Week 8 intro to python
Week 8 intro to python
brianjihoonlee
 
RubyConf China 2015 - Rails off assets pipeline
RubyConf China 2015 - Rails off assets pipeline
Florian Dutey
 
Joomla! multiplied - How to run Multi-Sites - JandBeyond 2014
Joomla! multiplied - How to run Multi-Sites - JandBeyond 2014
Viktor Vogel
 
ASP.NET Core - Phillosophies, Processes and Tooling
ASP.NET Core - Phillosophies, Processes and Tooling
💻 Spencer Schneidenbach
 
The future of templating and frameworks
The future of templating and frameworks
Filip Bruun Bech-Larsen
 
Terminal Happiness
Terminal Happiness
Liam Dempsey
 
Whats next in templating
Whats next in templating
Filip Bruun Bech-Larsen
 
Rails Vs CakePHP
Rails Vs CakePHP
Gautam Rege
 
SharePoint Fest Chicago 2017 - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Chicago 2017 - SharePoint Framework, Angular & Azure Functions
Sébastien Levert
 
Joomla as a mobile App backend - ideas, examples and experiences
Joomla as a mobile App backend - ideas, examples and experiences
Andy_Gaskell
 
Go Programming Language - Learning The Go Lang way
Go Programming Language - Learning The Go Lang way
IndicThreads
 
Performance and Abstractions
Performance and Abstractions
Metosin Oy
 
So you think you can scale
So you think you can scale
Dan Beil
 
Reason React
Reason React
Arnar Þór Sveinsson
 
My Contributor Story
My Contributor Story
Marko Heijnen
 
WordPress Development Environments
WordPress Development Environments
Josh Cummings
 
How to write bad code using C#
How to write bad code using C#
Wekoslav Stefanovski
 
Building mobile apps with PhoneGap and Backbone
Building mobile apps with PhoneGap and Backbone
Troy Miles
 
Flask and Paramiko for Python VA
Flask and Paramiko for Python VA
Enrique Valenzuela
 
διαγων ιστορ γεν 4
διαγων ιστορ γεν 4
fan Mel
 
Traveling: The Places I've Been
Traveling: The Places I've Been
MikeLikesIE
 

More Related Content

What's hot (20)

Making CLI app in ruby
Making CLI app in ruby
Huy Do
 
Week 8 intro to python
Week 8 intro to python
brianjihoonlee
 
RubyConf China 2015 - Rails off assets pipeline
RubyConf China 2015 - Rails off assets pipeline
Florian Dutey
 
Joomla! multiplied - How to run Multi-Sites - JandBeyond 2014
Joomla! multiplied - How to run Multi-Sites - JandBeyond 2014
Viktor Vogel
 
ASP.NET Core - Phillosophies, Processes and Tooling
ASP.NET Core - Phillosophies, Processes and Tooling
💻 Spencer Schneidenbach
 
The future of templating and frameworks
The future of templating and frameworks
Filip Bruun Bech-Larsen
 
Terminal Happiness
Terminal Happiness
Liam Dempsey
 
Whats next in templating
Whats next in templating
Filip Bruun Bech-Larsen
 
Rails Vs CakePHP
Rails Vs CakePHP
Gautam Rege
 
SharePoint Fest Chicago 2017 - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Chicago 2017 - SharePoint Framework, Angular & Azure Functions
Sébastien Levert
 
Joomla as a mobile App backend - ideas, examples and experiences
Joomla as a mobile App backend - ideas, examples and experiences
Andy_Gaskell
 
Go Programming Language - Learning The Go Lang way
Go Programming Language - Learning The Go Lang way
IndicThreads
 
Performance and Abstractions
Performance and Abstractions
Metosin Oy
 
So you think you can scale
So you think you can scale
Dan Beil
 
Reason React
Reason React
Arnar Þór Sveinsson
 
My Contributor Story
My Contributor Story
Marko Heijnen
 
WordPress Development Environments
WordPress Development Environments
Josh Cummings
 
How to write bad code using C#
How to write bad code using C#
Wekoslav Stefanovski
 
Building mobile apps with PhoneGap and Backbone
Building mobile apps with PhoneGap and Backbone
Troy Miles
 
Flask and Paramiko for Python VA
Flask and Paramiko for Python VA
Enrique Valenzuela
 
Making CLI app in ruby
Making CLI app in ruby
Huy Do
 
Week 8 intro to python
Week 8 intro to python
brianjihoonlee
 
RubyConf China 2015 - Rails off assets pipeline
RubyConf China 2015 - Rails off assets pipeline
Florian Dutey
 
Joomla! multiplied - How to run Multi-Sites - JandBeyond 2014
Joomla! multiplied - How to run Multi-Sites - JandBeyond 2014
Viktor Vogel
 
ASP.NET Core - Phillosophies, Processes and Tooling
ASP.NET Core - Phillosophies, Processes and Tooling
💻 Spencer Schneidenbach
 
Terminal Happiness
Terminal Happiness
Liam Dempsey
 
Rails Vs CakePHP
Rails Vs CakePHP
Gautam Rege
 
SharePoint Fest Chicago 2017 - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Chicago 2017 - SharePoint Framework, Angular & Azure Functions
Sébastien Levert
 
Joomla as a mobile App backend - ideas, examples and experiences
Joomla as a mobile App backend - ideas, examples and experiences
Andy_Gaskell
 
Go Programming Language - Learning The Go Lang way
Go Programming Language - Learning The Go Lang way
IndicThreads
 
Performance and Abstractions
Performance and Abstractions
Metosin Oy
 
So you think you can scale
So you think you can scale
Dan Beil
 
My Contributor Story
My Contributor Story
Marko Heijnen
 
WordPress Development Environments
WordPress Development Environments
Josh Cummings
 
Building mobile apps with PhoneGap and Backbone
Building mobile apps with PhoneGap and Backbone
Troy Miles
 
Flask and Paramiko for Python VA
Flask and Paramiko for Python VA
Enrique Valenzuela
 

Viewers also liked (19)

διαγων ιστορ γεν 4
διαγων ιστορ γεν 4
fan Mel
 
Traveling: The Places I've Been
Traveling: The Places I've Been
MikeLikesIE
 
Q3 Evaluation
Q3 Evaluation
jadziaedmb
 
Moz trip 09
Moz trip 09
Darryl Meekins
 
Indicadores de medición
Indicadores de medición
Andres Martin
 
Manual de serviço ch125 spacy (1993) mskv8931 p chassi
Manual de serviço ch125 spacy (1993) mskv8931 p chassi
Thiago Huari
 
pnsr_the_power_of_people_report
pnsr_the_power_of_people_report
Sharon Czarnek
 
Подумай о новогодних подарках СЕЙЧАС
Подумай о новогодних подарках СЕЙЧАС
Dmitry Myskov
 
Selection of an appropriate classification technique for coastal biomass mapp...
Selection of an appropriate classification technique for coastal biomass mapp...
Urooj Saeed
 
Biotecnología
Biotecnología
Joshebeth Reyes
 
Khan Saeed Doc Files In PDF
Khan Saeed Doc Files In PDF
Khan Saeed afridi
 
Tecnologías de punta
Tecnologías de punta
damarys12345
 
Presentation - Omega-3 PUFAs and Metabolic Syndrome
Presentation - Omega-3 PUFAs and Metabolic Syndrome
Josh Nooner
 
Resume
Resume
Rajiv Ranjan
 
Humor business
Humor business
Anand singh
 
Service: Meine kleine Kundenreise nach Texas. @smcst
Service: Meine kleine Kundenreise nach Texas. @smcst
Andreas H. Bock
 
Demand analysis
Demand analysis
Simran Kaur
 
skoleledermøde
skoleledermøde
citadeljazz
 
Fotografico
Fotografico
George Martin
 
διαγων ιστορ γεν 4
διαγων ιστορ γεν 4
fan Mel
 
Traveling: The Places I've Been
Traveling: The Places I've Been
MikeLikesIE
 
Indicadores de medición
Indicadores de medición
Andres Martin
 
Manual de serviço ch125 spacy (1993) mskv8931 p chassi
Manual de serviço ch125 spacy (1993) mskv8931 p chassi
Thiago Huari
 
pnsr_the_power_of_people_report
pnsr_the_power_of_people_report
Sharon Czarnek
 
Подумай о новогодних подарках СЕЙЧАС
Подумай о новогодних подарках СЕЙЧАС
Dmitry Myskov
 
Selection of an appropriate classification technique for coastal biomass mapp...
Selection of an appropriate classification technique for coastal biomass mapp...
Urooj Saeed
 
Tecnologías de punta
Tecnologías de punta
damarys12345
 
Presentation - Omega-3 PUFAs and Metabolic Syndrome
Presentation - Omega-3 PUFAs and Metabolic Syndrome
Josh Nooner
 
Service: Meine kleine Kundenreise nach Texas. @smcst
Service: Meine kleine Kundenreise nach Texas. @smcst
Andreas H. Bock
 
Ad

Similar to Metaprogramming Go (20)

Go from a PHP Perspective
Go from a PHP Perspective
Barry Jones
 
Introduction to GoLang
Introduction to GoLang
NVISIA
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb
Leo Zhou
 
Advantages of golang development services & 10 most used go frameworks
Advantages of golang development services & 10 most used go frameworks
Katy Slemon
 
Enterprise 2020
Enterprise 2020
Siarhei Hladkou
 
Python develoopment company for custom applications development with a wealth...
Python develoopment company for custom applications development with a wealth...
Flexsin
 
Entrepreneur’s guide to programming
Entrepreneur’s guide to programming
Chris Callahan
 
Python Django Intro V0.1
Python Django Intro V0.1
Udi Bauman
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
Exploring metaprogramming using Ruby language
Exploring metaprogramming using Ruby language
Harshal Hayatnagarkar
 
Introduction to Go language
Introduction to Go language
Tzar Umang
 
Programming paradigm and web programming
Programming paradigm and web programming
Mohammad Kamrul Hasan
 
Why Your Business Should Leverage Python App Development in 2023.pptx
Why Your Business Should Leverage Python App Development in 2023.pptx
OnGraph Technologies Pvt. Ltd.
 
8 Top Web Development Programming Languages in 2023.pdf
8 Top Web Development Programming Languages in 2023.pdf
Baek Yongsun
 
0506-django-web-framework-for-python.pdf
0506-django-web-framework-for-python.pdf
radhianiedjan1
 
The Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web Development
twopoint718
 
Why is Python preferred over other Programming languages pptt.pptx
Why is Python preferred over other Programming languages pptt.pptx
VisiontrekCommunicat
 
Software Programming with Python II.pptx
Software Programming with Python II.pptx
GevitaChinnaiah
 
Ready, set, go! An introduction to the Go programming language
Ready, set, go! An introduction to the Go programming language
RTigger
 
Reasons to Choose Python Web Development for Your Business.pdf
Reasons to Choose Python Web Development for Your Business.pdf
Softprodigy
 
Go from a PHP Perspective
Go from a PHP Perspective
Barry Jones
 
Introduction to GoLang
Introduction to GoLang
NVISIA
 
1.6 米嘉 gobuildweb
1.6 米嘉 gobuildweb
Leo Zhou
 
Advantages of golang development services & 10 most used go frameworks
Advantages of golang development services & 10 most used go frameworks
Katy Slemon
 
Python develoopment company for custom applications development with a wealth...
Python develoopment company for custom applications development with a wealth...
Flexsin
 
Entrepreneur’s guide to programming
Entrepreneur’s guide to programming
Chris Callahan
 
Python Django Intro V0.1
Python Django Intro V0.1
Udi Bauman
 
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Building a high-performance, scalable ML & NLP platform with Python, Sheer El...
Pôle Systematic Paris-Region
 
Exploring metaprogramming using Ruby language
Exploring metaprogramming using Ruby language
Harshal Hayatnagarkar
 
Introduction to Go language
Introduction to Go language
Tzar Umang
 
Programming paradigm and web programming
Programming paradigm and web programming
Mohammad Kamrul Hasan
 
Why Your Business Should Leverage Python App Development in 2023.pptx
Why Your Business Should Leverage Python App Development in 2023.pptx
OnGraph Technologies Pvt. Ltd.
 
8 Top Web Development Programming Languages in 2023.pdf
8 Top Web Development Programming Languages in 2023.pdf
Baek Yongsun
 
0506-django-web-framework-for-python.pdf
0506-django-web-framework-for-python.pdf
radhianiedjan1
 
The Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web Development
twopoint718
 
Why is Python preferred over other Programming languages pptt.pptx
Why is Python preferred over other Programming languages pptt.pptx
VisiontrekCommunicat
 
Software Programming with Python II.pptx
Software Programming with Python II.pptx
GevitaChinnaiah
 
Ready, set, go! An introduction to the Go programming language
Ready, set, go! An introduction to the Go programming language
RTigger
 
Reasons to Choose Python Web Development for Your Business.pdf
Reasons to Choose Python Web Development for Your Business.pdf
Softprodigy
 
Ad

More from Weng Wei (12)

Fastest golang ORM implemented with generic
Fastest golang ORM implemented with generic
Weng Wei
 
About Caching
About Caching
Weng Wei
 
谈谈缓存
谈谈缓存
Weng Wei
 
用PY实现"Go元编程"
用PY实现"Go元编程"
Weng Wei
 
Go for web
Go for web
Weng Wei
 
Go for web
Go for web
Weng Wei
 
论使用Python开发推荐引擎的优越性
论使用Python开发推荐引擎的优越性
Weng Wei
 
Zalora php to_go
Zalora php to_go
Weng Wei
 
A byte of git
A byte of git
Weng Wei
 
My understanding of Git
My understanding of Git
Weng Wei
 
Sharding
Sharding
Weng Wei
 
Inside Zend Framework
Inside Zend Framework
Weng Wei
 
Fastest golang ORM implemented with generic
Fastest golang ORM implemented with generic
Weng Wei
 
About Caching
About Caching
Weng Wei
 
谈谈缓存
谈谈缓存
Weng Wei
 
用PY实现"Go元编程"
用PY实现"Go元编程"
Weng Wei
 
Go for web
Go for web
Weng Wei
 
Go for web
Go for web
Weng Wei
 
论使用Python开发推荐引擎的优越性
论使用Python开发推荐引擎的优越性
Weng Wei
 
Zalora php to_go
Zalora php to_go
Weng Wei
 
A byte of git
A byte of git
Weng Wei
 
My understanding of Git
My understanding of Git
Weng Wei
 
Inside Zend Framework
Inside Zend Framework
Weng Wei
 

Recently uploaded (20)

FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
“From Enterprise to Makers: Driving Vision AI Innovation at the Extreme Edge,...
Edge AI and Vision Alliance
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 

Metaprogramming Go

  • 3. About Me • Weng Wei, Chinese in Singapore • Full-stack developer • .net -> python -> go • Passionate about code & life
  • 4. 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
  • 5. 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
  • 6. C Macro #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next)
  • 7. 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
  • 8. How hard working gopher solve generic with py?
  • 10. • StringSet& IntSet has exact code structure, only types are different • Classic usage of generic
  • 14. auto generate • MakeFile? • Real-time file monitor: github.com/gorakhargosh/ watchdog
  • 20. Same struct definition • CRUD • Full-text search • User Permission • RPC
  • 21. Lots of boilerplate code for obj passing, value assign, display etc
  • 26. • Generate them all! • Generated code won’t have typo
  • 28. 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
  • 39. YAML is very handy
  • 43. • Similar to ORM: change DSL from thrift to YAML • YAML • More flexible • Non-coder friendly
  • 44. API • Needed in different places: • web • mobile web • mobile client • Business are the same, but may need different wrapping
  • 49. We should be lazy about writing boilerplate wrapping code
  • 51. So, we have 300k80line of code, mostly generated
  • 52. • 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
  • 53. Customisation? • Generate once, and change • Generate whenever template changes: • Provide callback interface • Method overwriting
  • 56. • 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
  • 57. Challenge of MP • Choosing the right DSL • Provide both: • high level of abstraction • low level customization • www.joelonsoftware.com/articles/ LeakyAbstractions.html
  • 58. 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
  • 59. 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?
  • 60. –Ken Thompson “When in doubt, use brute force.”
  • 61. 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
  • 62. Q & A