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 modify the value of an integer through a pointer inside a function.
func changeValue(n [1]) { *n = 20 } func main() { x := 10 changeValue(&x) fmt.Println(x) // Output: 20 }
The function parameter must be a pointer to int (*int) to modify the original variable's value.
Fill both blanks to make the increment function modify x correctly.
func increment(n [1]) { (*n)++ } func main() { x := 5 increment([2]x) fmt.Println(x) // Output: 6 }
The function parameter must be a pointer to int (*int), and the argument must be the address of x using &x.
Fill both blanks to create a function that swaps two integers using pointers.
func swap(a, b [1]) { temp := *a *a = *b *b = temp } func main() { x, y := 1, 2 swap([2]x, &y) fmt.Println(x, y) // Output: 2 1 }
The function parameters must be pointers to int (*int), and the arguments must be addresses of variables using &.
Fill all three blanks to create a function that doubles the value of an integer using a pointer and returns the new value.
func doubleValue(n [1]) [2] { *n = *n * 2 return [3] } func main() { x := 7 result := doubleValue(&x) fmt.Println(result, x) // Output: 14 14 }
The function parameter is a pointer to int (*int), the return type is int, and the function returns the dereferenced value *n.