xxxxxxxxxx
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract StructLifecycle {
struct Book {
string title;
string author;
uint price;
}
Book book;
constructor() {
setBookAuto();
}
function setBookAuto() private {
book.title = "Learn Solidity";
book.author = "Max Cavalera";
book.price = 1000;
}
function setBookManual() public {
book = Book('Learn Golang', 'Jamal Cavalera', 5000);
}
function getTitle() public view returns (string memory) {
return book.title;
}
function getAuthor() public view returns (string memory) {
return book.author;
}
function getPrice() public view returns (uint) {
return book.price;
}
}
xxxxxxxxxx
contract MyStructs {
struct NFT {
string name;
uint256 dna;
}
//creating nftList of array of strct NFT
NFT[] public nftList;
function addNFT(string memory _name, uint256 _dna) public {
// NFT memory newNFT;
// newNFT.name = _name;
// newNFT.dna = _dna;
NFT memory newNFT = NFT(_name, _dna);
nftList.push(newNFT);
}
xxxxxxxxxx
struct StructName {
uint8 text1,
uint32 text2,
uint8[3] textlist
}
StructName MyStruct = StructName(10, 300, [5, 7, 8])
// MyStruct.text1 => 10
// MyStruct.text2 => 300
// MyStruct.textlist => [5, 7, 8]