کار با Mapping، Struct و Array در Solidity

ایجاد شده توسط دکتر مصطفی جلیلی در آموزش برنامه نویسی سالیدیتی 7 مه 2025
اشتراک گذاری

کار با Mapping، Struct و Array در سالیدیتی


در این بخش با سه ساختار بسیار مهم داده‌ای در سالیدیتی آشنا می‌شویم که برای ساخت برنامه‌های بلاکچینی واقعی ضروری هستند:




Mapping در سالیدیتی


Mapping چیست؟


mapping نوعی ساختار داده‌ای مشابه دیکشنری یا هَش‌مپ (HashMap) در سایر زبان‌هاست. با آن می‌توانید یک کلید (Key) را به یک مقدار (Value) نگاشت کنید.


ساختار کلی:


mapping(KeyType => ValueType) public myMap;

مثال:


mapping(address => uint) public balances;
function updateBalance(uint amount) public {
balances[msg.sender] = amount;
}

در این مثال:
- هر آدرس دارای یک موجودی (uint) است.
msg.sender به آدرس فراخواننده تابع اشاره دارد.


نکات مهم در مورد mapping:
mappings قابل iteration (تکرار) نیستند.
- مقدار اولیه هر کلید تعریف نشده، اما برابر صفر پیش‌فرض نوع داده است.
- مناسب برای دسترسی سریع و مدیریت داده‌های مبتنی بر آدرس یا شناسه.




Struct در سالیدیتی


Struct چیست؟


struct روشی برای تعریف نوع داده سفارشی با چندین فیلد مختلف است، مشابه object یا struct در زبان‌های دیگر.


تعریف و استفاده:


struct User {
string name;
uint age;
bool isActive;
}
User public user1;
function createUser(string memory _name, uint _age) public {
user1 = User(_name, _age, true);
}

تعریف لیست از Struct:


User[] public users;
function addUser(string memory _name, uint _age) public {
users.push(User(_name, _age, true));
}

Struct تو در تو:


struct Profile {
string username;
address wallet;
mapping(string => uint) balances;
}

توجه: اگر در Struct از mapping استفاده کنید، نمی‌توانید مستقیماً از آن آرایه بسازید.




Array در سالیدیتی (آرایه‌ها)


تعریف و انواع آرایه :


uint[] public numbers;             // آرایه دینامیک
uint[5] public fixedArray; // آرایه با اندازه ثابت

عملیات رایج:


// اضافه کردن
numbers.push(10);

// خواندن مقدار
uint first = numbers[0];

// به‌روزرسانی مقدار
numbers[1] = 20;

// طول آرایه
uint len = numbers.length;

// حذف مقدار آخر
numbers.pop();

حذف مقدار خاص:


سالیدیتی متد remove(index) ندارد. برای حذف مقدار باید مقدار مورد نظر را جایگزین و سپس pop کنید، یا از آرایه‌ی جدید استفاده کنید.




ترکیب Mapping + Struct + Array


ساختارهای داده‌ای پیچیده اغلب ترکیبی از این سه هستند:


مثال عملی:


struct Product {
string name;
uint price;
}
mapping(uint => Product) public products;
uint public productCount;
function addProduct(string memory _name, uint _price) public {
products[productCount] = Product(_name, _price);
productCount++;
}

در این مثال:
- هر محصول با یک uint شناسه‌گذاری شده.
- جزئیات هر محصول در یک struct ذخیره می‌شود.
- همه محصولات در یک mapping نگهداری می‌شوند.




جمع‌بندی


در این بخش با سه ابزار بسیار مهم mapping، struct و array  برای مدیریت داده‌ها در سالیدیتی آشنا شدیم.

نظرات (0)

اشتراک گذاری

این پست را با دیگران به اشتراک بگذارید

تنظیمات GDPR

When you visit any of our websites, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and manage your preferences. Please note, that blocking some types of cookies may impact your experience of the site and the services we are able to offer.