我想在智能合约的声明行中初始化我的映射。我想知道最好的做法是什么?我试过以下,但Remix给我错误:
mapping(address _addr) public view myMap = [ addr-1 : true, addr-2 : false, addr-3 : true ];
您需要首先声明变量,然后在单独的行中为其赋值。
pragma solidity ^0.8;
contract MyContract {
mapping(address => bool) public myMap;
constructor() {
myMap[address(0x123)] = true;
myMap[address(0x456)] = false;
myMap[address(0x789)] = true;
}
}