内容简介:V 语言作者今天
V 语言作者今天 发布 了 V 语言的首个可用版本(预构建的二进制文件即将推出)。
源码获取地址: https://github.com/vlang/v/releases/tag/v0.0.12
作者提供了使用 V 语言开发的应用示例。
V语言是一个集合了 Go 的简单和 Rust 的安全特性的新语言。
主要特性:
- 快速编译(编译器只有 400kb,而且无第三方依赖)
- 安全
- C/C++ 转换
示例代码:
数据库访问:
struct User { /* ... */ }
struct Post { /* ... */ }
struct DB { /* ... */ }
struct Repo <T> {
db DB
}
fn new_repo<T>(db DB) Repo {
return Repo<T>{db: db}
}
fn (r Repo) find_by_id(id int) T? { // `?` means the function returns an optional
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) or {
eprintln('User not found')
return
}
post := posts_repo.find_by_id(1) or {
eprintln('Post not found')
return
}
}
网络开发:
struct Story {
title string
}
// Fetches top HN stories in 8 coroutines
fn main() {
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json')?
ids := json.decode([]int, resp.body)?
mut cursor := 0
for _ in 0..8 {
go fn() {
for {
lock { // Without this lock the program will not compile
if cursor >= ids.len {
break
}
id := ids[cursor]
cursor++
}
resp := http.get('https://hacker-news.firebaseio.com/v0/item/$id.json')?
story := json.decode(Story, resp.body)?
println(story.title)
}
}()
}
runtime.wait() // Waits for all coroutines to finish
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Apache OpenOffice 4.1.5 发布,可用 41 种语言
- D 语言(DLang)2.089.1 发布,细节改进提升可用性
- 构建生产环境可用的高可用kubernetes集群
- 维基百科 – Sparql查询获取dbpedia可用的所有可用电影
- 可用性高达5个9!支付系统高可用架构设计实战
- 浪潮InCloud OpenStack:度量可用性“三维”,实现高可用云环境
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
创意,未来的工作方式
方军 / 中信出版社 / 2016-11-20 / 58.00元
知识工作者已成过去,创意工作者才是未来 工作的本质是创意 纵观我们身处的世界,除了自然美景,世间或伟大或平凡的事物,几乎都是人观念革新的产物,它们多数是我们在工作过程中群体创意的产物。 从工业时代到知识时代,大多数人通过掌握新知、持续学习,获得社会的认可和回报;但进入以大数据、人工智能、机器人为标志的新时代,单纯的学习已经不能满足社会对人的要求。算法和机器人正在取代人类很多重复性......一起来看看 《创意,未来的工作方式》 这本书的介绍吧!