加入收藏 | 设为首页 | 会员中心 | 我要投稿 宁德站长网 (https://www.0593zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

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

发布时间:2019-06-24 11:47:16 所属栏目:优化 来源:佚名
导读:编程语言 V 的作者今天发布了 V 的首个可用版本(预构建的二进制文件即将推出)。 源码获取地址:https://github.com/vlang/v/releases/tag/v0.0.12 作者展示的使用 V 开发的应用示例。 V 是一个集合了 Go 的简单和 Rust 的安全特性的新语言。 主要特性:

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

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

编程语言 V 的作者今天发布了 V 的首个可用版本(预构建的二进制文件即将推出)。

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

源码获取地址:https://github.com/vlang/v/releases/tag/v0.0.12

作者展示的使用 V 开发的应用示例。

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

V 是一个集合了 Go 的简单和 Rust 的安全特性的新语言。

主要特性:

  • 快速编译(编译器只有 400kb,而且无第三方依赖)
  • 安全
  • C/C++ 转换

示例代码:

数据库访问:

  1. struct User { /* ... */ } 
  2. struct Post { /* ... */ } 
  3. struct DB   { /* ... */ } 
  4.  
  5. struct Repo <T> { 
  6.     db DB 
  7.  
  8. fn new_repo<T>(db DB) Repo { 
  9.     return Repo<T>{db: db} 
  10.  
  11. fn (r Repo) find_by_id(id int) T? { // `?` means the function returns an optional 
  12.     table_name := T.name // in this example getting the name of the type gives us the table name 
  13.     return r.db.query_one<T>('select * from $table_name where id = ?', id) 
  14.  
  15. fn main() { 
  16.     db := new_db() 
  17.     users_repo := new_repo<User>(db) 
  18.     posts_repo := new_repo<Post>(db) 
  19.     user := users_repo.find_by_id(1) or { 
  20.         eprintln('User not found') 
  21.         return 
  22.     } 
  23.     post := posts_repo.find_by_id(1) or { 
  24.         eprintln('Post not found') 
  25.         return 
  26.     } 
  27. }  

网络开发:

  1. struct Story { 
  2.     title string 
  3.  
  4. // Fetches top HN stories in 8 coroutines  
  5. fn main() { 
  6.     resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json')? 
  7.     ids := json.decode([]int, resp.body)? 
  8.     mut cursor := 0 
  9.     for _ in 0..8 { 
  10.         go fn() { 
  11.             for  { 
  12.                 lock { // Without this lock the program will not compile  
  13.                     if cursor >= ids.len { 
  14.                         break 
  15.                     } 
  16.                     id := ids[cursor] 
  17.                     cursor++ 
  18.                 } 
  19.                 resp := http.get('https://hacker-news.firebaseio.com/v0/item/$id.json')?  
  20.                 story := json.decode(Story, resp.body)? 
  21.                 println(story.title) 
  22.             } 
  23.         }() 
  24.     } 
  25.     runtime.wait() // Waits for all coroutines to finish  
  26. }  

(编辑:宁德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!