V Language Learning Notes
Table of Contents :
Introduction: vlang, the language of the future
vlang, for front-end, back-end, big data, blockchain, artificial intelligence, pervades every aspect of society. Beating other programming languages and unifying the world in 2050. It even became the lingua franca of the world, allowing mankind to rebuild the legendary Tower of Babel and advance in technology by leaps and bounds ......
--- Quoted from People's Daily 2050
Preface
V语言
, Vlang, the programming language for hackers.
rust and zig are good, but they are too strict and collegial.
V is more like javascript, a quick start language, not so many rules and regulations, fast to start, and of course fast.
V home page many publicity has not yet landed (such as C automatically to V), by many people called false propaganda, such as :
First go to publicity, and then to achieve, is the right strategy, human beings are a community of imagination.
Most people believe because they see, and some people see because they believe.
Just as, belief in communism may be in the minority, but there are always very few people who really believe, and the fire of a star can start a prairie fire. Without the Communist Party, there would be no new China.
Maybe computer mathematicians will find V a bit frivolous, but I believe hackers will like this language.
Thinking is the first thing to do, dare to think, dare to try, maybe this is the difference between art and mathematics. Is a programming language art or mathematics? This is the eternal question.
I think the biggest advantage of V is that it blurs the boundary between language design and program development.
It compiles itself, and in three or five seconds, it can be recompiled, and the code is clear.
The author of V writes in the V code directory structure:
I tried to make the compiler and the vlib code as simple and readable as possible.
One of the goals of V is to be open to developers with varying levels of compiler development experience. The compiler does not need to be a black box full of magic that only a few people can understand.
The V compiler is modular and can be used by other applications. It is located at
cmd/v/
andvlib/v/
.The most important and useful command when working with the V compiler is
v self
. It rebuilds the V compiler.The structure of the compiler is very simple and has three different steps.
Parsing/generating AST (
v.parser
) => checking type (v.checker
) => generating C/JavaScript/machine code (v.gen
)
One might think :
But I think that this simplicity is the biggest advantage of V.
Do not think of V as a traditional bulky programming language, the compiler is an obscure book of heaven.
Think of it as a web front-end framework, like Vue, with a low barrier to entry, and everyone can participate.
If you have any problems, you can change it.
Do it yourself, you have enough to eat.
The beautiful new world is not sitting and waiting, it is created by the working people with their hands.
v self
Compile yourself
V can easily compile itself with v self
. On my 2015 version of Apple laptop, it takes 5.63 seconds to compile once.
I recommend installing it in source form.
git clone git@github.com:vlang/v.git
make
sudo ./v symlink
If you have any ideas, go ahead and modify the source code of the compiler or standard library directly.
Then use v self
and compile it yourself. Once you have your requirements sorted out, commit and merge to the official source code.
Today I'm just learning V and I've launched 3 merge requests (1, 2, 3 ) with a suggestion for language improvement: optional explicit declaration of support for an interface.
I'm not good at English, but with the translator at DeepL, it's easy to communicate with international friends in depth on Chinese to English.
V Writing scripts
v init
After initializing the project, I need a compilation script.
V can write this script instead of bash.
My compile script is as follows.
#!/usr/bin/env -S v run
import v.vmod
fn sh(cmd string){
println("❯ $cmd")
print(execute_or_exit(cmd).output)
}
vm := vmod.decode( @VMOD_FILE ) or { panic(err) }
name := vm.name
sh('v -autofree -prod ${name}.v')
suffix := $if windows { '.exe' } $else { '' }
src := name + suffix
bin := join_path_single('bin',src)
mkdir('bin') or {}
mv(src, bin)?
println(' '+bin)
Monitor file changes and restart the run automatically
It's my habit to write and run once, and monitoring file changes and restarting the run automatically is what I need.
v comes with v watch run xx.v
to do this.
I created a script to facilitate the use of ./dev.sh
as follows.
#!/usr/bin/env bash
DIR=$(dirname $(realpath "$0"))
cd $DIR
set -ex
NAME=$(cat v.mod|grep name|awk -F\' '{print $2}')
SH="v watch run $NAME.v"
exec $SH \
-i '.git' \
-i 'bin' \
-c -s --before "printf \"\e[90m❯ ${SH}\e[0m\n\""
The project template is available at rmw-link/rmw-v-template