V语言一个集合了Go的简单和Rust的安全特性的新语言。
主要特性:
快速编译(编译器只有400kb,而且无第三方依赖)安全C/C++转换示例代码:
数据库访问:
structUser{/*...*/}structPost{/*...*/}structDB{/*...*/}structRepo<T>{dbDB}fnnew_repo<T>(dbDB)Repo{returnRepo<T>{db:db}}fn(rRepo)find_by_id(idint)T?{//`?`meansthefunctionreturnsanoptionaltable_name:=T.name//inthisexamplegettingthenameofthetypegivesusthetablenamereturnr.db.query_one<T>('select*from$table_namewhereid=?',id)}fnmain(){db:=new_db()users_repo:=new_repo<User>(db)posts_repo:=new_repo<Post>(db)user:=users_repo.find_by_id(1)or{eprintln('Usernotfound')return}post:=posts_repo.find_by_id(1)or{eprintln('Postnotfound')return}}网络开发:
structStory{titlestring}//FetchestopHNstoriesin8coroutinesfnmain(){resp:=http.get('https://hacker-news.firebaseio.com/v0/topstories.json')?ids:=json.decode([]int,resp.body)?mutcursor:=0for_in0..8{gofn(){for{lock{//Withoutthislocktheprogramwillnotcompileifcursor>=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()//Waitsforallcoroutinestofinish}
评论