I would like to initialise my mapping in the declaration line in a smart contract. I was wondering what is the best practice? I have tried the following but Remix is giving me errors:
mapping(address _addr) public view myMap = [ addr-1 : true, addr-2 : false, addr-3 : true ];
You need to declare the variable first, and then assign it values on separate lines.
pragma solidity ^0.8;
contract MyContract {
mapping(address => bool) public myMap;
constructor() {
myMap[address(0x123)] = true;
myMap[address(0x456)] = false;
myMap[address(0x789)] = true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With