Understanding Mappings in Solidity
📖 Scenario: You are creating a simple blockchain contract to store and retrieve user balances. This contract uses a mapping to link each user's address to their balance.
🎯 Goal: Build a Solidity contract that uses a mapping to store balances for addresses, set a balance for one user, and then retrieve and display that balance.
📋 What You'll Learn
Create a
mapping named balances that links address to uintCreate a
uint variable named initialBalance and set it to 100Write a function
setBalance that sets the initialBalance for the sender's address in balancesWrite a function
getBalance that returns the balance of the sender's addressPrint the balance of the sender after setting it
💡 Why This Matters
🌍 Real World
Mappings are used in blockchain contracts to store data linked to user addresses, like balances, permissions, or votes.
💼 Career
Understanding mappings is essential for blockchain developers to manage user data efficiently and securely in smart contracts.
Progress0 / 4 steps