vlang 试用

vlang 是最近出来的一门编程语言,集成了rust,golang, 等语言的特性,轻量、简洁、编译
快速,详细的比价参数可以参考官方文档

安装

目前尽管官方提供了linux以及mac 的二进制文件,但是发现运行还是有点问题,所以最好的方法还是使用源码编译

  • 源码编译v 编译器
 
mac:
git clone https://github.com/vlang/v
cd v/compiler
make
linux:
这个就有稍有点问题,主要是gcc 版本,我使用的centos7,需要使用sc 进行gcc 多版本的处理
安装sc gcc 版本:
sudo yum install centos-release-scl
sudo yum install devtoolset-7-gcc*
scl enable devtoolset-7 bash
gcc -v
编译与mac 一样
git clone https://github.com/vlang/v
cd v/compiler
make

简单使用

  • 简单代码

    hello_world.v

import http
import os
fn main() {
    names := ['Sam', 'Peter']
    printnames(names)
    printenv()
    fetchbaidu()
}
// for each demo
fn printnames(n []string) {
     for item in n {
         println(item)
     }
}
// os env
fn printenv(){
    println(os.getenv('USERNAME'))
}
// http demo
fn fetchbaidu() {
    resp := http.get('https://www.baidu.com')
    println(resp)
}
// function demo
fn printusername(n string) {
    println(n)
}
 
  • 代码说明
    以上代码包含了简单的http、env、function、array 的代码,从以上可以看出代码还是很清晰的
  • 运行&&编译
    实际上运行就会有一个编译的二进制文件 运行命令
    v run hello_world.v
    效果:
 
v run hello_world.v 
============running hello_world==============================
Sam
Peter
dalong
<!DOCTYPE html><!--STATUS OK-->
<html>
<head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge">
        <link rel="dns-prefetch" href="//s1.bdstatic.com"/>
        <link rel="dns-prefetch" href="//t1.baidu.com"/>
        <link rel="dns-prefetch" href="//t2.baidu.com"/>
        <link rel="dns-prefetch" href="//t3.baidu.com"/>
        <link rel="dns-prefetch" href="//t10.baidu.com"/>
        <link rel="dns-prefetch" href="//t11.baidu.com"/>
        <link rel="dns-prefetch" href="//t12.baidu.com"/>
        <link rel="dns-prefetch" href="//b1.bdstatic.com"/>
.....
 
 

生成生产二进制文件:
v -prod hello_world.v

说明

v 已经内置了好多可选的模块json http ui,基本是够用了,同时支持的数据类型也是比较多,有自己的模块概念(类似rust)
看看后边的发展,期望在ide ,模块仓库上有提升,还有就是跨平台的语言构建(目前测试看着还是不支持的,尽管官方文档
有描述)。

参考资料

https://vlang.io/#faq
https://github.com/vlang/v

原文地址:https://www.cnblogs.com/rongfengliang/p/11102465.html