Junior — Middle
For which data types can their contents be directly modified without using pointers?
sobes.tech AI
Answer from AI
In Go, you can directly modify the contents of the following variable types without using pointers:
- Primitive types:
int,float64,bool,string(although strings in Go are immutable, you can assign new values) - Arrays (if they are not passed by pointer)
- Structures (if they are not passed by pointer)
For example, if you have a variable of type int or a structure, you can change its fields or value directly:
var x int = 10
x = 20 // direct modification
type Point struct {
X, Y int
}
p := Point{X: 1, Y: 2}
p.X = 5 // direct modification of the structure's field
Pointers are needed when you want to change the value of a variable passed to a function or work with large structures without copying.