// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/*
Data locations
storage 状态变量
memory 局部变量
calldata 类似memory,但只能用于输入的参数中
*/
contract DataLocations {
struct MyStruct {
uint foo;
string text;
}
mapping(address => MyStruct) public myStructs;
function examples() external {
myStructs[msg.sender] = MyStruct({foo: 123, text: "bar"});
// storage 状态变量,方法结束后可以保存修改结果,即 myStructs[msg.sender] 为 MyStruct({foo: 123, text: "foo"});
// MyStruct storage myStruct = myStructs[msg.sender];
// myStruct.text = "foo";
// memory 局部变量,方法结束后不可以保存修改结果,即 myStructs[msg.sender] 为 MyStruct({foo: 123, text: "bar"});
MyStruct memory readOnly = myStructs[msg.sender];
readOnly.foo = 456;
}
}