V语言强势登顶GitHub TOP1,发布了首个可用版本!


640?wx_fmt=jpeg

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

640?wx_fmt=jpeg

整理 | 郭芮

出品 | CSDN(ID:CSDNnews)

长久以来,编程语言在语法、语义和标准库等方面都存在着千差万别,使得程序员在选择时不得不面临着差异化等难题。自然选择下,就会有旧语言的淘汰(PHP 是个意外,至今还存在)和新语言的诞生。在 JetBrains 最新发布的《2019 开发人员生态系统现状》报告中,Java、Python、C/C#、JavaScript 等主流的编程语言在历经实践考验后依然是开发者们的心头好。

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

640?wx_fmt=png

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

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

640?wx_fmt=gif

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

640?wx_fmt=jpeg

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

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


std::vector 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  {    db DB}fn new_repo(db DB) Repo {    return Repo{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('select * from $table_name where id = ?', id)}fn main() {    db := new_db()    users_repo := new_repo(db)    posts_repo := new_repo(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  {
    db DB
}

fn new_repo(db DB) Repo {
    return Repo{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('select * from $table_name where id = ?', id)
}

fn main() {
    db := new_db()
    users_repo := new_repo(db)
    posts_repo := new_repo(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 语言还是需要不断的发展,得到开发者的广泛应用才能焕发生机,也才能有望助力程序员做到真正的“人剑合一”。

640?

1.在你正觉得STM32F3还缺点儿啥时,STM32G4来了!

2.戏说二十余国产操作系统之天理命数

3.在做5G项目的嵌入式工程师,请留意META-DX1!

4.LabVIEW这么“反人类”的东西,到底哪些人在用?月薪2万+的人,笑笑不说话…

5.鸿蒙将至,安卓安否?

6.5G基站为何建得比4G多?

640?wx_fmt=gif

免责声明:本文系网络转载,版权归原作者所有。如涉及作品版权问题,请与我们联系,我们将根据您提供的版权证明材料确认版权并支付稿酬或者删除内容。