Original title: "Introduction to Solidity Minimalist: 6. Mapping Type mapping"
Original author: 0xAA
p>
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.
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:

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:

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

