v语言初体验小结
作者:王李 时间:2022-09-22 01:02:51
最近github上开源了一个"新语言"vlang,火的不得了,我不信,于是乎,尝试了一下,真香。
以下内存均来自https://www.v-lang.cnv语言文档 by 20190701
ubuntu安装vlang
依赖安装
这里采用的是ubuntu的机器,centos的会报错,暂时没有去操作
查看机器型号
$ uname -a
Linux liwang 4.15.0-54-generic #58-Ubuntu SMP Mon Jun 24 10:55:24 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
$
安装必要依赖 gcc 和 make
$ sudo apt install gcc make -y
$
下载编译安装v
$ git clone https://github.com/vlang/v
$ cd v/
$ make
$ sudo ln -s ~/v/v /usr/local/bin/
测试
$ v
V 0.1.10
Use Ctrl-D or `exit` to exit
For now you have to use println() to print values, this will be fixed soon
>>> print ('hello world')
hello world
>>> $
$
Hello world
代码
$ cat hello.v
fn main() {
/*
* 注意: v 语言中 字符串不允许 使用双引号,至少在 v1.0版本不行
*/
println('Hello World')
}
$
编译运行
编译.v文件
$ v hello.v
$ ./hello && rm -f hello
Hello World
$
编译运行
直接使用 v run file.v 可以编译并且运行二进制文件
$ v run hello.v
============ running hello ============
Hello World
$
函数
语法/案例
函数和golang类似 语法
fn 函数名称 (形参) 返回值 {
函数体
}
例子
$ cat func1.v
fn v_add(x,y int) int {
z := x + y
return z
}
fn main() {
a := 1
b := 2
c := v_add(a,b)
println(c)
}
$
编译运行
$ v run func1.v
============ running func1 ============
3
$
数组
数组基本用法
代码
$ cat arrays.v
fn main() {
// 定义数组
test_array := [1,2,3]
// 打印数组
println(test_array)
// 输出数组长度
println(test_array.len)
// 判断数组是否包含 1
println(test_array.contains(1))
// 判断数组是否包含 4
println(test_array.contains(4))
}
$
编译执行
$ v run arrays.v
============ running arrays ============
[1, 2, 3]
true
false
$
可变数组
默认数组定义好之后,是不能修改其长度的,而在其前面加上关键字 mut ,则可以实现变长数组,例如
代码
$ cat arrays_1.v
fn main() {
// 定义可变数组
mut other_arrays := [1,2,3]
// 打印数组长度
println('数组变化前长度')
println(other_arrays.len)
// 打印数组内容
println('数组内容')
println(other_arrays)
// 追加数组值
other_arrays << 4
// 打印数组长度
println('数组追加后长度')
println(other_arrays.len)
// 打印数组内容
println('数组内容')
println(other_arrays)
}
$
编译运行
ps: 目前的方法我只找到了追加,还未找到删除,[尴尬--]
$ v run arrays_1.v
============ running arrays_1 ============
数组变化前长度
数组内容
[1, 2, 3]
数组追加后长度
数组内容
[1, 2, 3, 4]
$
未完待续。。。
来源:https://www.cnblogs.com/wang-li/p/11116166.html
标签:v语言
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python去除扩展名的实例讲解
2022-05-08 18:10:49
python3实现域名查询和whois查询功能
2023-09-19 19:35:12
Pandas中DataFrame对象转置(交换行列)
2023-06-11 05:01:45
![](https://img.aspxhome.com/file/2023/0/63520_0s.png)
python中pip安装、升级以及升级固定的包
2021-07-08 02:29:11
![](https://img.aspxhome.com/file/2023/9/65559_0s.png)
使用css给图片添加阴影入门篇
2009-07-06 14:38:00
详谈python在windows中的文件路径问题
2023-12-16 22:02:34
MYSQL初学者使用指南[适用自己安装mysql者]
2007-08-06 14:53:00
![](https://img.aspxhome.com/file/UploadPic/up/2007080615020296.jpg)
Python Beautiful Soup模块使用教程详解
2021-07-21 16:00:10
![](https://img.aspxhome.com/file/2023/9/117889_0s.png)
简单上手Python中装饰器的使用
2023-06-20 19:20:06
百万级asp分页存储过程代码(ver2.0)
2007-12-17 13:13:00
利用python的socket发送http(s)请求方法示例
2022-06-06 08:33:39
![](https://img.aspxhome.com/file/2023/1/68871_0s.png)
TensorFlow中tf.batch_matmul()的用法
2022-06-06 10:33:37
ASP 三层架构 Error处理类
2011-03-16 11:06:00
Python Celery定时任务详细讲解
2022-08-12 08:25:21
![](https://img.aspxhome.com/file/2023/1/107611_0s.png)
windows中python实现自动化部署
2023-06-24 16:04:14
![](https://img.aspxhome.com/file/2023/0/134520_0s.jpg)
opencv 图像加法与图像融合的实现代码
2021-07-20 14:57:30
![](https://img.aspxhome.com/file/2023/2/91952_0s.jpg)
python实现与redis交互操作详解
2022-07-07 17:37:18
![](https://img.aspxhome.com/file/2023/0/130620_0s.png)
Python验证的50个常见正则表达式
2023-07-11 02:58:57
详解python-docx处理Word必备工具
2023-10-03 03:15:08
python源码剖析之PyObject详解
2023-08-02 14:07:03
![](https://img.aspxhome.com/file/2023/5/61975_0s.png)