Skip to content

Instantly share code, notes, and snippets.

@ZakriaJanjua
Created January 31, 2022 10:45
Show Gist options
  • Save ZakriaJanjua/e281272efc0634a1da29cd9b223210da to your computer and use it in GitHub Desktop.
Save ZakriaJanjua/e281272efc0634a1da29cd9b223210da to your computer and use it in GitHub Desktop.
mapping and nested mapping in solidity
pragma solidity >=0.7.0 <0.9.0;
contract Mapping {
mapping(uint => Book) public books;
//nested mapping
mapping(address => mapping(uint8 => Book)) public myBooks;
struct Book {
string author;
string title;
}
function addBook(uint _id, string memory _author, string memory _title) public {
books[_id] = Book(_author, _title);
}
function addMyBook(uint8 _id, string memory _author, string memory _title) public {
myBooks[msg.sender][_id] = Book(_author, _title);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment