设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 开源资讯 查看内容

安全的全新编程语言V发布首个可用版本

2019-6-23 18:02| 发布者: joejoe0332| 查看: 567| 评论: 0|原作者: oschina|来自: oschina

摘要: 编程语言 V 的作者今天发布了 V 的首个可用版本(预构建的二进制文件即将推出)。V是一个集合了Go的简单和Rust的安全特性的新语言。

编程语言 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 
} 

酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部