歡迎您光臨本站 註冊首頁

安全的全新V語言發布首個可用版本

←手機掃碼閱讀     admin @ 2019-06-23 , reply:0

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 
} 

[admin ]

來源:OsChina
連結:https://www.oschina.net/news/107663/v-lang-source-code-released
安全的全新V語言發布首個可用版本已經有417次圍觀

http://coctec.com/news/all/show-post-207894.html