forked from vlang/edu-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dadb61
commit 7eb172c
Showing
3 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
Next Level Program | ||
================== | ||
|
||
Now, for a slightly more complex version of the `Hello World` program. This is | ||
a copy of one of the examples on the [V Home Page](https://vlang.io). | ||
```v | ||
fn main() { | ||
areas := ['game', 'web', 'tools', 'science', 'systems', | ||
'embedded', 'drivers', 'GUI', 'mobile'] | ||
for area in areas { | ||
println('Hello, ${area} developers!') | ||
} | ||
} | ||
``` | ||
|
||
In this version of the program, several more ideas are introduced. | ||
|
||
V variables are declared by giving the name, followed by `:=` then a value to | ||
set both the initial value of the variable, as well as it's type. | ||
|
||
In the example, | ||
```v | ||
areas := ['game', 'web', 'tools', 'science', 'systems', | ||
'embedded', 'drivers', 'GUI', 'mobile'] | ||
``` | ||
`areas` is declared as the name of a variable, and is assigned the type `[]string` | ||
(meaning an array of strings), with the initial values of the array set to the | ||
strings inside the `[]` characters. | ||
|
||
Unlike other languages, V does not allow you to create a variable without | ||
assigning a value, as the value sets the variable type. | ||
|
||
The 2nd new thing is the [`for`](https://github.com/vlang/v/blob/master/doc/docs.md#for-loop) | ||
loop, which is the only looping statement in V. | ||
|
||
For this variation, it is used to loop over the values in the `areas` array, so | ||
that each one can be used in the `println` statement. | ||
```v-ignore | ||
for area in areas { | ||
``` | ||
This means "for every string in the `areas` string array, assign the value to a | ||
variable named `area`, then execute the statements inside the `{}`. | ||
|
||
The `println` statement is also different in this example: | ||
```v | ||
println('Hello, ${area} developers!') | ||
``` | ||
Here, V uses a new concept called "string interpolation". This basically means | ||
that V will take whatever is inside `${}` in the string, treat it as V code, | ||
and use the value as if it was part of the string when it is printed. | ||
|
||
For this specific case, it means "use the value of the variable named `area`", | ||
and the result is a string that changes every time through the loop, as the | ||
value of `area` changes. | ||
|
||
The output from running this program is | ||
``` | ||
Hello, game developers! | ||
Hello, web developers! | ||
Hello, tools developers! | ||
Hello, science developers! | ||
Hello, systems developers! | ||
Hello, embedded developers! | ||
Hello, drivers developers! | ||
Hello, GUI developers! | ||
Hello, mobile developers! | ||
``` |