这个结合了 Go 和 Rust 特性的新语言 V,正式发布了首个可用版本。



整理 | 郭芮

出品 | CSDN(ID:CSDNnews)

长久以来,编程语言在语法、语义和标准库等方面都存在着千差万别,使得程序员在选择时不得不面临着差异化等难题。自然选择下,就会有旧语言的淘汰(PHP
是个意外,至今还存在)和新语言的诞生。在 JetBrains 最新发布的《2019 开发人员生态系统现状
<http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650722924&idx=1&sn=acef1cd23b7e44af032b0d70b885cc26&chksm=bea6a3bf89d12aa9955588321edadc1a0157945be33a817a2637c76e530f7ce201d466a163a1&scene=21#wechat_redirect>
》报告中,Java、Python、C/C#、JavaScript 等主流的编程语言在历经实践考验后依然是开发者们的心头好。

 

而本文的主角 V 语言,在千呼万唤之后,终于于近日开源了,并正式发布了首个可用版本(预构建的二进制文件也即将推出)!其一经发布,便强势登顶 GitHub
TOP1,引来开发者们的热议围观。

 



(https://github.com/vlang/v/releases/tag/v0.0.12)

 

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

 



 

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

 



(作者展示的应用示例:V 语言建立的 macOS Demo)

 

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

 
 
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、数据库访问:

 
 
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、网络开发:

 
 
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++
等手动和半自动的管理方式还是更省心一些的。

 

那么开发者们怎么看?

 

 

@三川走刀口:还是要得到开发者认可,但是对于安卓开发好像没用?

 

@淡定的龙哥:Go语言同父异母的弟弟?

 

@Heisenber哥:语言特性只是一方面,生态也很重要。

 

@王的凝视:这个新语言提出来是为了解决什么问题?每种语言都有适合场景,如果没有合适场景迟早也要被淘汰。

 

@楚小欢:执行效率比C高应该不可能,C现在都被认为是汇编语言,本身语义也十分接近汇编。别的语言只要有高级点的特性,效率就不可能超过C。

 

总之,这个新生的 V 语言还是需要不断的发展,得到开发者的广泛应用才能焕发生机,也才能有望助力程序员做到真正的“人剑合一”。

程序员转行学什么语言?

https://edu.csdn.net/topic/ai30?utm_source=csdn_bw
<https://edu.csdn.net/topic/ai30?utm_source=csdn_bw>

【END】



 热 文 推 荐 

☞小程序技术演进史
<http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650723170&idx=1&sn=877682527e063906e1c1755d1018ca23&chksm=bea6a2b189d12ba756cc18425eb9085ed28ecf717f8b53a4e35a8fb4e0b6f04c0dac5b57a921&scene=21#wechat_redirect>

☞5G 兴起,物联网安全危机四伏
<http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650723129&idx=2&sn=3fda6e76f04a31c9d9a960048b82f2c7&chksm=bea6a2ea89d12bfc95609f7e11fff6f73008ad5e5d974a02be7c774b5902e3f2e73692589488&scene=21#wechat_redirect>

☞写代码不严谨,我就不配当程序员?
<http://mp.weixin.qq.com/s?__biz=MjM5MjAwODM4MA==&mid=2650723170&idx=3&sn=4032180cb2c979f6d81ce5e94d8a6a71&chksm=bea6a2b189d12ba75390cf0156709f7413ffd33a87ae76a05373a4f4a1f68622f83a61808786&scene=21#wechat_redirect>

☞华为最强自研 NPU 问世,麒麟 810 “抛弃”寒武纪
<https://blog.csdn.net/csdnsevenn/article/details/93426320>

☞LinkedIn最新报告: 区块链成职位需求增长最快领域, 这些地区对区块链人才渴求度最高……
<http://mp.weixin.qq.com/s?__biz=MzU2MTE1NDk2Mg==&mid=2247496120&idx=1&sn=ebf0dc65de49be204f51de906b573510&chksm=fc7faf45cb082653bfb0d145c9d12f3144021888efa2d76af85dc1dc7c962fcb048e58e7f90f&scene=21#wechat_redirect>

☞中文NLP的分词真有必要吗?李纪为团队四项任务评测一探究竟 | ACL 2019
<https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/93424708>

☞6月技术福利限时免费领 <https://blog.csdn.net/dQCFKyQDXYm3F8rB0/article/details/93426194>

☞搞不懂SDN?那是因为你没看这个小故事…
<https://blog.csdn.net/FL63Zv9Zou86950w/article/details/93377349>

她说:程序员离开电脑就是 “废物” ! <https://blog.csdn.net/csdnnews/article/details/92082695>

点击阅读原文,输入关键词,搜索CSDN文章。

你点的每个“在看”,我都认真当成了喜欢

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信