Vscripts
V語言還可以用來寫系統(tǒng)shell腳本,借助簡潔的語法,寫shell腳本還是比較舒服的,而且還可以是跨平臺的
V腳本的文件名后綴為 .vsh
區(qū)別于.v文件,在.vsh中:
不用定義主模塊
不用定義主函數(shù)
不用導(dǎo)入os模塊,調(diào)用os模塊函數(shù)時,可以省略os前綴,就像使用內(nèi)置函數(shù)那樣
直接像shell腳本那樣寫,代碼從頭開始運行
script.vsh
for _ in 0..5 {
println('V script')
}
println('deploying...')
println(ls('.'))
println('')
mv('v.exe', 'bin/v.exe')
rm('tmp.c')
mkdir('name')
create('foo.txt')
println(ls('.'))
println('')
println('Removing name and foo.txt')
println('')
rmdir('name')
rm('foo.txt')
println(ls('.'))
編譯,運行:
v script.vsh && ./script
或者直接運行:
v run script.vsh
具體的os模塊常用的函數(shù)可以參考標(biāo)準(zhǔn)庫章節(jié)的介紹