# Variables

Variablen können in V mit dem `:=` Operator deklariert und initialisiert werden. Das ist die einzige Möglichkeit Variablen in V zu erzeugen, daraus folgt, dass alle Variablen in V immer einen initialen Wert haben. Der Typ der Variable wird aus dem Wert auf der rechten Seite der Zuweisung abgeleitet. Variablen in V sind immutable per default.

```go
age := 23               // int
name := 'Alice'         // string
is_adult := age > 21    // bool

println(age_str)        // 23
println(name)           // Alice
println(is_adult)       // true
```

**Merke:** Variablen können nur innerhalb einer Funktion definiert werden. Es gibt in V keine globalen Variablen und auch keinen globalen Programmzustand.

Um den Wert einer Variable zu ändern muss diese mutable sein. Dies kann durch hinzufügen von `mut` zur Deklaration erreicht werden. Einen neuen Variablenwert kann man dann mit `=` setzen.

```go
mut age := 20       // we declare the mutable variable age and assign it to the value 20.
println(age)        // 20
age = 21            // we assign a new value to age
println(age)        // 21
```

Das Weglassen des `mut` Schlüsselworts hier resultiert in einem Fehler beim Kompilieren, denn der Wert einer immutable Variable kann nicht verändert werden.

```go
fn main() {
    age = 20
    println(age)
}
```

Der obenstehende Code würde ebenfalls in einem Fehler beim kompilieren resultieren, da die Variable `age` hier nicht definiert ist,

```go
fn main() {
    mut age := 20       // we declare the mutable variable age and assign it to the value 20.
    println(age)        // 20
    age := 21           // ERROR
}
```

Hier würde `age := 21` einen Fehler beim kompilieren erzeugen, denn `age` ist hier schon definiert und kann nicht neu deklariert werden.

Eine Variable anlegen: `:=`.

Einen neuen Wert zuweisen: `=`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://v-community.gitbook.io/v-by-example/de/examples/section_1/variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
