1. // SPDX-License-Identifier: GPL-3.0
    2. pragma solidity >=0.7.0 <0.9.0;
    3. /*
    4. Data locations
    5. storage 状态变量
    6. memory 局部变量
    7. calldata 类似memory,但只能用于输入的参数中
    8. */
    9. contract DataLocations {
    10. struct MyStruct {
    11. uint foo;
    12. string text;
    13. }
    14. mapping(address => MyStruct) public myStructs;
    15. function examples() external {
    16. myStructs[msg.sender] = MyStruct({foo: 123, text: "bar"});
    17. // storage 状态变量,方法结束后可以保存修改结果,即 myStructs[msg.sender] 为 MyStruct({foo: 123, text: "foo"});
    18. // MyStruct storage myStruct = myStructs[msg.sender];
    19. // myStruct.text = "foo";
    20. // memory 局部变量,方法结束后不可以保存修改结果,即 myStructs[msg.sender] 为 MyStruct({foo: 123, text: "bar"});
    21. MyStruct memory readOnly = myStructs[msg.sender];
    22. readOnly.foo = 456;
    23. }
    24. }