Complete the code to declare a pointer to an integer variable.
var x int = 10 var p [1] = &x
In Go, a pointer to an int is declared as *int. The & operator gets the address of x.
Complete the code to change the value of an integer through its pointer.
var x int = 5 var p *int = &x *p [1] 20
To assign a new value through a pointer, use =. The expression *p = 20 changes the value of x to 20.
Fix the error in the code to correctly swap two integers using pointers.
func swap(a, b [1] int) {
temp := *a
*a = *b
*b = temp
}The function parameters should be pointers to int, declared as *int. This allows swapping the values at the addresses.
Fill both blanks to create a map where keys are strings and values are pointers to integers.
m := map[string][1]{} m["score"] = [2]
The map values should be pointers to int, so *int. To assign a pointer, use the address operator & on a variable like x.
Fill all three blanks to define a struct with a pointer field, create an instance, and assign a value through the pointer.
type Person struct {
age [1]
}
p := Person{age: nil}
var a int = 30
p.age = [2]
*[3] = 35The struct field age is a pointer to int (*int). Assign the address of a to p.age. Then dereference p.age to change the value.