iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
✌️

Hello World in V

に公開

What is V Language?

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

V is a recently emerged programming language.
The official website states:

The V Programming Language
Simple, fast, safe, compiled. For developing maintainable software.

It seems to be simple, fast, and safe.

My Environment

OS: macOS 12.3 21E230 x86_64
Device: MacBook Pro 2019 13-inch
CPU: Intel i7-8569U (8) @ 2.80GHz
Memory: 16GB

Installation

Documentation
https://github.com/vlang/v/blob/master/doc/docs.md

As of now (2022/04/05), it seems building from source is the only method available.
I will proceed with building it.
git, a C compiler (any of tcc, gcc, or clang), and make are required.

For Linux or macOS:

Terminal
git clone https://github.com/vlang/v
cd v
make

For Windows:

Terminal
git clone https://github.com/vlang/v
cd v
make.bat -tcc

If you want to use v globally:

Terminal
sudo ./v symlink

This will allow you to use v from anywhere.

v

You can see descriptions of commands with v help.

Terminal
$ v help
V is a tool for managing V source code.

Usage:
   v [options] [command] [arguments]

Examples:
   v hello.v                 Compile the file `hello.v` and output it as `hello` or `hello.exe`.
   v run hello.v             Same as above but also run the produced executable immediately after compilation.
   v -cg run hello.v         Same as above, but make debugging easier (in case your program crashes).
   v -o h.c hello.v          Translate `hello.v` to `h.c`. Do not compile further.
   v -o - hello.v            Translate `hello.v` and output the C source code to stdout. Do not compile further.

   v watch hello.v           Re-does the same compilation, when a source code change is detected.
                             The program is only compiled, not run.
   v watch run hello.v       Re-runs the same `hello.v` file, when a source code change is detected.

V supports the following commands:
* New project scaffolding:
   new               Setup the file structure for a V project (in a sub folder).
   init              Setup the file structure for an already existing V project.

* Ordinary development:
   run               Compile and run a V program.
   test              Run all test files in the provided directory.
   fmt               Format the V code provided.
   vet               Report suspicious code constructs.
   doc               Generate the documentation for a V module.
   vlib-docs         Generate and open the documentation of all the vlib modules.
   repl              Run the REPL.
   watch             Re-compile/re-run a source file, each time it is changed.

* Installation/self updating:
   symlink           Create a symbolic link for V.
   up                Run the V self-updater.
   self [-prod]      Run the V self-compiler, use -prod to optimize compilation.
   version           Print the version text and exits.

* Module/package management:
   install           Install a module from VPM.
   remove            Remove a module that was installed from VPM.
   search            Search for a module from VPM.
   update            Update an installed module from VPM.
   upgrade           Upgrade all the outdated modules.
   list              List all installed modules.
   outdated          List installed modules that need updates.
   show              Display information about a module on vpm

* Others:
   doctor            Display some useful info about your system to help reporting bugs.
   translate         Translate C code to V (coming soon in 0.3).
   tracev            Produce a tracing version of the v compiler.
                     Use `tracev yourfile.v` when the compiler panics.
                     NB: `tracev` is much slower and more verbose than ordinary `v`

Use "v help <command>" for more information about a command, example: `v help build`, `v help build-c`, `v help build-native`
Use "v help other" to see less frequently used commands.
Use "v help topics" to see a list of all known help topics.

Note: Help is required to write more help topics.
Only build, new, init, doc, fmt, vet, run, test, watch, search, install, remove, update, bin2v, check-md are properly documented currently.
Terminal
v

You can enter an interactive mode like Python by running the command above.

Terminal
$ v
 ____    ____
 \   \  /   /  |  Welcome to the V REPL (for help with V itself, type  exit , then run  v help ).
  \   \/   /   |  Note: the REPL is highly experimental. For best V experience, use a text editor,
   \      /    |  save your code in a  main.v  file and execute:  v run main.v
    \    /     |  V 0.2.4 0bd8fbc . Use  list  to see the accumulated program so far.
     \__/      |  Use Ctrl-C or  exit  to exit, or  help  to see other available commands.

Basic arithmetic looks like this:

Terminal
>>> 1 + 1
2
>>> 3 * 4
12
>>> 0.004 - 0.001
0.003
>>> 1 / 3
0
>>> 1.0 / 3.0
0.3333333

Calculations work normally, but it feels incredibly slow. It feels like an addition takes about 2 seconds. Since the V language is apparently transpiled to C once, I assume a build process is being performed every time.

Hello, World

Extensions

I use VS Code, so I will install the extension.
It comes with syntax highlighting and snippets.
Unfortunately, as of now (2022/04/05), it doesn't seem to have a formatting feature.
It doesn't point out syntax errors and the suggestions are weak, so it's not very reliable...

https://marketplace.visualstudio.com/items?itemName=vlanguage.vscode-vlang

Creating a Project

You can easily create a project using the v command installed earlier.

Terminal
$ v new
Input your project name: hello_world
Input your project description:
Input your project version: (0.0.0)
Input your project license: (MIT)
Initialising ...
Complete!

It's similar to npm init.
When executed, a new directory named hello_world is created.
Open this in VS Code.

It should look something like this.
(The nested .gitignore is due to another extension.)

Execution

The program to output "Hello, World" is already created, so all that's left is to run it.

Terminal
$ v run ./hello_world.v
Hello World!

Done.

The code should look something like this.

./hello_world.v
module main

fn main() {
	println('Hello World!')
}

It feels like a mix of Rust and Go.

Conclusion

We've successfully done "Hello, World" in V.
If you want to know more detailed specifications, please refer to the official documentation.
Strings, arrays, and for-loops have unique specifications and are interesting.

https://github.com/vlang/v/blob/master/doc/docs.md

GitHubで編集を提案

Discussion