如何用春节假期造一个最快的
泛型Go ORM?
翁伟 Wuvist @ Ola Chat
关于我
• 85前+、居住新加坡、追求极致的处女座
• 搞过 .net / python / php / go等等
• https://github.com/olachat/gola
• 基于Go 1.18实现
• 已经是我在不同语言下搞的第N个ORM
• 最近问了下前司的同事,之前的两个Go ORM还一直在线
上使用,并团队有持续改进
春节就搞定?
当然只是搞定了初版 POC
距离功能“完备”,还有十几个todo
但确实应该是“世界最快”。。。
Go与泛型
知乎不是一个技术网站,技术内容质
量参差不齐
“Go不需要泛型”
“有interface就够了”
“大道至简 ”
什么是泛型 generic?
• Generic programming is a style of computer
programming in which algorithms are written in terms of
types to-be-specified-later that are then instantiated
when needed for specific types provided as parameters.
• 泛型是程序设计语言的一种风格或范式。泛型允许程序员
在实现算法逻辑时,延迟声明具体的数据类型,即实例化
时才把具体类似作为参数传递。
• 泛型解决的是将固定算法作于于不同类型的问题
• 这于接口:不同类型提供同一接口有本质不同
• 算法的可复用性其实非常重要
• 最典型的例子是数组排序
• 序列化marshal等等也是
• https://docs.google.com/document/d/1vrAy9gMpMoS3uaV
phB32uVXX4pi-
HnNjkMEgyAHX4N4/view#heading=h.7hy69baqv6xo
Go当然需要泛型!优先级的问题
Fastest golang ORM implemented with generic
Fastest golang ORM implemented with generic
Fastest golang ORM implemented with generic
那有泛型加持的gola是怎么样?
• select id, title from blogs
• 能够以强类型的方式指定select字段
• 无需使用字符串形式去指定select字段名
• inline的声明匿名类型,并使用
• 彻底杜绝调用了未被select字段的可能
• 应该没有别的ORM实现了这两点
有没有LINQ的感觉?
• select * from blogs where user_id = 123 and is_pinned =
true order by id desc limit 10
Fastest golang ORM implemented with generic
感知索引以提供函数补全
• blogs.Select[blogs.Blog]().WhereUserIdEQ(123)
.AndIsPinnedEQ(true).OrderBy(blogs.IdDesc)
.Limit(10, 0)
• 源自于:
• KEY `user_pinned` (`user_id`, `is_pinned`)
索引 与 补全
• 能够“面向编辑器补全”写出来的强类型查询,必然有索引
支持
• 彻底规避“忘了建索引”的线上故障
• 发现查询写不了?
• 考虑一下是否要增加索引?
• 确实建了索引,那就手写SQL
• 彻底强类型,规避低级错误
+
• 面向编辑器补全开发,愉快编码
+
• 查询必有索引,减少线上问题烦恼
=
• 少加班
gola 与 索引
• 索引对于数据库使用极其重要
• 数据库其实可以没有表结构schema
• https://backchannel.org/blog/friendfeed-schemaless-
mysql
• 但OLTP数据库不可以没有索引!
看回行业历史发展
各种数据库使用范式,均没有针对索
引提供支持:
* Transaction Script
* Table Module
* Domain Model
* ActiveRecord
Source: Patterns of Enterprise Application Architecture by Martin Fowler
IAQ: Index-Aware Query
索引感知查询
希望gola提供的索引感知查询能够成
为新的范式
IAQ其实才是整个gola实现最难的部分
耗费了我整个春节…
Fastest golang ORM implemented with generic
gola大量依赖于代码生成
• blogs.Id / blogs.Title 实际上是生成出来的类型struct
• 黑魔法?
Fastest golang ORM implemented with generic
影响性能的不是泛型,而是接口设计
用了反射还快?
• 使用读取对象类型、属性、判断接口是很快的
• 反射的读往往是简单的unsafe包操作
• 还可以基于类型做缓存
• 使用反射做赋值才慢
性能其实不重要
• 重要的是开发效率
• 强类型补全有能带来极大的愉悦
• 重要的是代码质量
• SQL不能瞎写,要考虑索引
• 极致性能与高效率的功能并不冲突
• 相反可以相辅相成
• 设计做得好,性能自然极致
与1000位技术大咖同行
结交全球大咖 拓展业务合作 缓解中年焦虑 生活多样精彩
加入小红花,你可以
把握技术潮流 打造个人品牌
https://github.com/olachat/gola
火热填坑中~求Star~求关注~
Q & A

More Related Content

PPT
2012,我的技术之选
PPTX
Go 語言基礎簡介
PDF
Python First Class
PPTX
Go for web
PDF
用 Python 打造你自己的 summly
PPTX
Codeigniter 3.0 之 30 分鐘就上手
PPTX
大獅子與小獅子的分享
PPTX
Github初上手教學
2012,我的技术之选
Go 語言基礎簡介
Python First Class
Go for web
用 Python 打造你自己的 summly
Codeigniter 3.0 之 30 分鐘就上手
大獅子與小獅子的分享
Github初上手教學

Similar to Fastest golang ORM implemented with generic (13)

PPTX
与Python一路走来
PPTX
如何,高效利用搜索引擎+构建网络工具箱
PDF
Grpc + python.key
PPTX
构建网络工具箱
PDF
如何使用工具提高PHP项目的生产力
PDF
Adorable python
PDF
Golang
PDF
優雅的在 Emacs 中使用 git
PPTX
Ping Pong 2012/11/29 @NTU博理館
PDF
不妥協的河蟹之道
PPTX
Ping pong
KEY
Phonagp 简介
PPTX
01.python.开发最佳实践
与Python一路走来
如何,高效利用搜索引擎+构建网络工具箱
Grpc + python.key
构建网络工具箱
如何使用工具提高PHP项目的生产力
Adorable python
Golang
優雅的在 Emacs 中使用 git
Ping Pong 2012/11/29 @NTU博理館
不妥協的河蟹之道
Ping pong
Phonagp 简介
01.python.开发最佳实践
Ad

More from Weng Wei (14)

PDF
Migrate PHP E-Commerce Site to Go
PDF
About Caching
PDF
Metaprogramming Go
PDF
Why use Go for web development?
PDF
谈谈缓存
PPTX
用PY实现"Go元编程"
PDF
Go for web
PDF
Python to go
PDF
论使用Python开发推荐引擎的优越性
PDF
Zalora php to_go
PDF
A byte of git
KEY
My understanding of Git
KEY
Sharding
KEY
Inside Zend Framework
Migrate PHP E-Commerce Site to Go
About Caching
Metaprogramming Go
Why use Go for web development?
谈谈缓存
用PY实现"Go元编程"
Go for web
Python to go
论使用Python开发推荐引擎的优越性
Zalora php to_go
A byte of git
My understanding of Git
Sharding
Inside Zend Framework
Ad

Fastest golang ORM implemented with generic