"Hello World" in Solidity
March 21, 2022
The "Hello World" program is most likely the first thing you would learn when trying to learn a new language.
Here's what a "Hello World" smart contract in Solidity looks like:
What is Solidity?
Solidity is statically typed, object-oriented, and high-level language for building smart contracts.
Code Walkthrough
-
- Solidity files take the extension
.sol
// SPDX-License-Identifier: MIT
- specifies the license used by this contract. It's not mandatory but it's important since smart contracts are open source.
pragma solidity 0.8.0;
- specifies the version of solidity that should be used during the compilation of the contract. It's required for the code to run.
Other variations likepragma solidity ^0.8.0;
,pragma solidity >=0.8.0;
,pragma solidity <=0.8.0;
andpragma solidity >=0.4.22 <0.8.0;
can also be used to specify versions greater or equal to 0.8.0, versions greater or equal to version(same as the previous), versions less than or equal to 0.8.0 and versions greater than or equal to 0.4.22 but less than 0.8.0 respectively.
contract HelloWorld {...}
- creates a code block for writing the logic of the smart contract. This is similar to defining classes in object-oriented programming languages.
string public greeting = "Hello World";
- as a statically-typed language the data type needs to be specified. Here, a string is used.
Thepublic
keyword is a visibility modifier. And, thegreeting
is the variable name.public
makes it possible for the variablegreeting
to be called a getter functiongreeting()
- Solidity files take the extension
Conclusion
Congratulations!! you just wrote your first smart contract in solidity. Go ahead and try the code on Remix.
Just as this is your first, this is also our first post on here. First of many.
Good luck on your journey.
This was originally published on blockchaintotheworld.com