header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

A minimalist introduction to Solidity|Lecture 6: Mapping type mapping

Read this article in 4 Minutes
This article introduces hash tables in Solidity: the Mapping type.
Original title: "Introduction to Solidity Minimalist: 6. Mapping Type mapping"
Original author: 0xAA


I am re-learning solidity recently, to consolidate the details, and also write a "Simple Introduction to Solidity" for beginners (programmers can find other tutorials), and update 1-3 lectures every week.


All codes are open source on github: github.com/AmazingAng/WTFSolidity


In this lecture, we will introduce the hash table in solidity: the Mapping type.


Map Mapping


In the map, people can pass the key (Key) To query the corresponding value (Value), for example: query a person's wallet address through his id.


The format of the declaration mapping is  mapping(_KeyType => _ValueType), where _KeyType and _ValueType are the variable types of Key and Value respectively. Example:



Mapping rules


Rule 1: The mapped _KeyType can only choose the solidity default type, such as uint, address, etc., which cannot be used custom structure. And _ValueType can use a custom type. The following example will report an error because _KeyType uses our custom structure:



< /p>

Rule 2: The storage location of the mapping must be storage, so it can be used for the status variable of the contract and the stoage variable in the function. Cannot be used in parameters or return results of public functions.


Rule 3: If the mapping is declared as public, then solidity will automatically create a getter function for you, which can be queried by Key Value.


Rule 4: The syntax of the key-value pair added to the mapping is _Var[_Key] = _Value , where _Var It is the mapping variable name, and _Key and _Value correspond to the newly added key-value pair. Example:



Summary


In this lecture, we introduced the usage of the hash table in solidity - Mapping. So far, we have learned all the common variable types, and then we will learn about control flow if-else, while, etc.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit