Junior — Middle
What function does the `*` operator perform in Go, especially regarding pointers?
sobes.tech AI
Answer from AI
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 the variable is a pointer to a type.
Example:
var a int = 10
var p *int = &a // p is a pointer to a
fmt.Println(*p) // outputs 10 — the value that p points to
*p = 20 // changing the value of a through the pointer
fmt.Println(a) // outputs 20
Thus, * allows working with addresses and values in memory, providing low-level control over data.