Content deleted Content added
Struct names changed to avoid the interpretation of an insult and for clear generic neutrality |
adding for the sake of attribution |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 56:
== Syntax ==
=== Hello world ===
The [["Hello, World!" program]] in V:<ref name="MUO"/><ref name=":0">{{Cite web |title=V Documentation |url=https://docs.vlang.io/ |access-date=2025-08-25 |website=docs.vlang.io}} {{Free-content attribution|title = V Documentation| license statement URL =https://github.com/vlang/v?tab=MIT-1-ov-file| license=The MIT License|this = yes}}</ref>
<syntaxhighlight lang="v">
fn main() {
Line 64:
=== Variables ===
Variables are immutable by default and are defined using {{code|1=:=}} and a value. Use the {{code|mut}} [[reserved word]] (keyword) to make them mutable. Mutable variables can be assigned to using {{code|1==}}:{{sfn|Rao|2021|pp=28-40}}<ref name=":0" />
<syntaxhighlight lang="V">
Line 72:
</syntaxhighlight>
Redeclaring a variable, whether in an inner scope or in the same scope, is not allowed:{{sfn|Rao|2021|pp=28-40}}<ref name=":0" />
<syntaxhighlight lang="V">
Line 83:
=== Structs ===
Struct example:<ref name="Knott"/><ref name="section"/><ref name=":0" />
<syntaxhighlight lang="v">
struct Foo {
Line 104:
=== Heap structs ===
<syntaxhighlight lang="v">
struct Foo {
Line 126:
[[Method (computer programming)|Methods]] in V are functions defined with a receiver [[Parameter (computer programming)|argument]]. The receiver appears in its own argument list between the fn keyword and the method name. Methods must be in the same [[Modular programming|module]] as the receiver type.
The
struct Client {
enrolled bool
}
fn (x Client)
}
▲println(client_1.enrolled()) // "false"
▲println(client_2.enrolled()) // "true"
</syntaxhighlight>
=== Error handling ===
Result types may represent an error returned from a function. Result types are declared by prepending {{code|!}}: {{code|!Type}}
Optional types may represent {{code|none}}. Option types prepend {{code|?}} to the type name: {{code|?Type}}.<ref name="Knott"/><ref name="section"/>{{Sfn|Tsoukalos|2022}}<ref name=":0" />
<syntaxhighlight lang="V">
fn something(t string) !string {
Line 168 ⟶ 158:
</syntaxhighlight>
==See also==
|