0
0
Goprogramming~10 mins

Common pointer use cases in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a pointer to an integer variable.

Go
var x int = 10
var p [1] = &x
Drag options to blanks, or click blank then click option'
A*int
Bint*
C&int
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of '*int' for pointer type.
Using '&int' which is invalid syntax.
Using 'int*' which is not Go syntax.
2fill in blank
medium

Complete the code to change the value of an integer through its pointer.

Go
var x int = 5
var p *int = &x
*p [1] 20
Drag options to blanks, or click blank then click option'
A=
B==
C:=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which is a comparison, not assignment.
Using ':=' which declares a new variable, not assignment.
Using '!=' which is a comparison operator.
3fill in blank
hard

Fix the error in the code to correctly swap two integers using pointers.

Go
func swap(a, b [1] int) {
    temp := *a
    *a = *b
    *b = temp
}
Drag options to blanks, or click blank then click option'
A**
B&&
C&
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&int' which is invalid syntax.
Using '**int' which is a pointer to a pointer, not needed here.
Using '&&int' which is invalid.
4fill in blank
hard

Fill both blanks to create a map where keys are strings and values are pointers to integers.

Go
m := map[string][1]{}
m["score"] = [2]
Drag options to blanks, or click blank then click option'
A*int
B&x
Cint
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' as map value type instead of '*int'.
Assigning 'x' instead of '&x' to the map value.
Using '&int' which is invalid.
5fill in blank
hard

Fill all three blanks to define a struct with a pointer field, create an instance, and assign a value through the pointer.

Go
type Person struct {
    age [1]
}
p := Person{age: nil}
var a int = 30
p.age = [2]
*[3] = 35
Drag options to blanks, or click blank then click option'
A*int
B&a
Cp.age
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring age as int instead of *int.
Assigning 'a' instead of '&a' to p.age.
Dereferencing wrong variable instead of p.age.