Junior — Middle
Milline on `*` operaatori Go keeles, eriti viitadega seotud funktsioon?
sobes.tech AI
Vastus AI-lt
In the Go language, the * operator is used for working with pointers and performs two main functions:
-
Dereferencing a pointer — obtaining the value that the pointer points to.
-
Declaring a pointer — when declaring a variable with
*, it indicates that it is a pointer to a type.
Example:
var a int = 10
var p *int = &a // p is a pointer to a
fmt.Println(*p) // prints 10 — the value that p points to
*p = 20 // change the value of a via the pointer
fmt.Println(a) // prints 20
Thus, * allows working with addresses and values in memory, providing low-level control over data.