Structs
Explore how to create and utilize structs in Solidity to combine multiple data fields into a single variable. Understand how to implement ownership tracking by adding an owner address to your smart contract's registry, and learn to use the msg.sender field to assign ownership. This lesson helps you manage complex data structures essential for developing functional Ethereum smart contracts.
We'll cover the following...
So far, we’re only using built-in Solidity types in our smart contracts. But with Solidity, we can also define composite types containing multiple fields in one variable. To do this, we need to use structs, a feature we'll learn about in this lesson.
We'll first see how we can define and use structs. Then, we'll add a feature to our smart contract that will allow us to track who owns each name in our registry. An owner of a name will then have some extra privileges that we'll implement later in this lesson.
Structs in Solidity
Structs in Solidity allow for the creation of a new type that packs together multiple fields in one variable. It's somewhat similar to classes in other programming languages, but structs in Solidity don't have methods and don’t support inheritance.
Here, we define a struct with two fields: a string name and the numeric age field.
We don’t need to add a semicolon ...