0
0
Goprogramming~15 mins

Address and dereference operators in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Address and Dereference Operators
📖 Scenario: Imagine you are managing a simple inventory system where you need to update the quantity of an item using pointers. This helps you understand how to use address and dereference operators in Go.
🎯 Goal: You will create a variable for an item quantity, use a pointer to access its address, update the quantity through the pointer, and then print the updated quantity.
📋 What You'll Learn
Create an integer variable called quantity with the value 10.
Create a pointer variable called ptr that stores the address of quantity.
Use the dereference operator to update the value of quantity to 25 through the pointer ptr.
Print the updated value of quantity.
💡 Why This Matters
🌍 Real World
Pointers are used in real programs to efficiently update data without copying it, such as modifying inventory counts or managing resources.
💼 Career
Understanding pointers is important for system programming, performance optimization, and working with low-level data structures in Go.
Progress0 / 4 steps
1
Create the initial variable
Create an integer variable called quantity and set it to 10.
Go
Hint

Use := to declare and assign the variable in one line.

2
Create a pointer to the variable
Create a pointer variable called ptr that stores the address of quantity using the address operator &.
Go
Hint

Use &quantity to get the address of quantity.

3
Update the variable using the pointer
Use the dereference operator * on ptr to update the value of quantity to 25.
Go
Hint

Use *ptr = 25 to change the value at the address stored in ptr.

4
Print the updated value
Use fmt.Println to print the updated value of quantity.
Go
Hint

Remember to import fmt and use fmt.Println(quantity) to display the value.