V语言初探

V语言,感觉厉害。。。。。。
人家的Helloworld只需这样

fn main() {
	println('hello world')
}

就是短小精悍。

居然还有泛型,这个可是连我们可以到处GO的都没有。

struct Repo <T> {
	db DB
}
 
fn new_repo<T>(db DB) Repo<T> {
	return Repo<T>{db: db}
}
 
// This is a generic function. V will generate it for every type it's used with. 
fn (r Repo<T>) find_by_id(id int) T? {
	table_name := T.name // in this example getting the name of the type gives us the table name 
	return r.db.query_one<T>('select * from $table_name where id = ?', id)
}
 
fn main() {
	db := new_db()
	users_repo := new_repo<User>(db)
	posts_repo := new_repo<Post>(db)
	user := users_repo.find_by_id(1)?
	post := posts_repo.find_by_id(1)?
} 

V语言官网地址:https://vlang.io
V语言Github地址:https://github.com/vlang/v

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值