前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >V 语言强势登顶 GitHub TOP1,欲取 Go 而代之?

V 语言强势登顶 GitHub TOP1,欲取 Go 而代之?

作者头像
MySQL轻松学
发布2019-08-01 18:01:20
4410
发布2019-08-01 18:01:20
举报
文章被收录于专栏:MYSQL轻松学MYSQL轻松学

长久以来,编程语言在语法、语义和标准库等方面都存在着千差万别,使得程序员在选择时不得不面临着差异化等难题。自然选择下,就会有旧语言的淘汰(PHP 是个意外,至今还存在)和新语言的诞生。在 JetBrains 最新发布的《2019 开发人员生态系统现状》报告中,Java、Python、C/C#、JavaScript 等主流的编程语言在历经实践考验后依然是开发者们的心头好。 而本文的主角 V 语言,在千呼万唤之后,终于于近日开源了,并正式发布了首个可用版本(预构建的二进制文件也即将推出)!其一经发布,便强势登顶 GitHub TOP1,引来开发者们的热议围观。

https://github.com/vlang/v

根据介绍,V 是一种新型的静态编译型语言,可以“快速编译、安全且和 C/C++ 转换”,其提供了方便、快捷、安全的编程语言和工具包,还能够很好地服务于区块链技术。

V 语言作者 Reishi Saza 就表示,它是一种非常简单的语言,看官方文档 30 分钟就能完全掌握。而且,其编译器只有 400KB,无任何第三方依赖。

(作者展示的应用示例:V 语言建立的 macOS Demo) V 的核心 CPU 每秒可以编译大约 120 万行代码,这种速度是通过生成的机器代码和强大的模块化来实现的,但是目前仅支持 x64/Mach-O,预计到今年年底才能足够稳定。而在性能表现上,V 可以做到和 C 一样快,且能够翻译整个 C 或 C++ 项目,实现高达 400x 的编译速度。

代码语言:javascript
复制
std::vector<std::string> s;s.push_back("V is ");s.push_back("awesome");std::cout << s.size();mut s := []s << 'V is 's << 'awesome'println(s.len)s.push_back("V is ");s.push_back("awesome");std::cout << s.size(); mut s := []s << 'V is 's << 'awesome'println(s.len)

目前,整个 V 语言及其标准库小于 400 KB,开发者在 0.4 秒内就可以构建它。并且到今年年底,这个数字还将下降到大约 0.15 秒。 此外,开发者们还在官网上放出了部分示例代码。更多编译器函数介绍可参见官方网站:https://vlang.io/。 1、数据库访问:

代码语言:javascript
复制
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 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    }} 

2、网络开发:

代码语言:javascript
复制
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 }     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 } 

当然,目前V 语言的开发仍处于早期阶段,很多方面还不够完善,尤其是内存管理上还面临着与 Go 和 Rust 同样繁琐的生命期管理问题,但对比 C++ 等手动和半自动的管理方式还是更省心一些的。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MYSQL轻松学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云区块链服务平台 TBaaS
腾讯云区块链服务平台(Tencent Blockchain as a Service,简称TBaaS)致力于打造全球领先的企业级区块链技术平台,帮助客户、开发者及合作伙伴轻松创建和管理可托管、可扩展的区块链网络,助力产业协同发展。TBaaS 支持长安链·ChainMaker、Hyperledger Fabric等区块链底层平台,简化部署、运维及开发流程,实现业务快速上链,提升链上治理效率。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档