Solidity
A smart contract can be written in various programming languages such as Rust, Yul, and JavaScript. One programming language that is recommended and made for the purpose is Solidity. It is an object-oriented programming language. It is influenced by C++, Python, and JavaScript.
Hello World in Solidity
Letโs do a Hello World in Solidity and have a basic knowledge of the syntax.
The extension for a solidity file is .sol
. At the top of every solidity file, we define the license of the code. It is not mandatory but recommended, without it, the compiler will give you a warning. We comment down the license.
If you donโt want to define the license of the code or it is not open-source, you can use UNLICENSED
. Otherwise, you can use a variety of licenses for your code such as MIT, Apache-2.0, or GPL-2.0. You can find more on it here.
// SPDX-License-Identifier: MIT
After defining the license of the code, we define the compiler version of the solidity to use. It starts with the keyword pragma
. It is used to enable a certain feature of the compiler. After this, we have solidity then the version of the compiler. Currently, the latest version of solidity available is 0.8.13. You can always use the relational operator to define the version.
pragma solidity >=0.7.0 <0.9.0;
After this, we have to define the contract with the keyword contract
followed by the name of the contract. After that, we have curly brackets. Within those, we define the code for the contract.
contract helloworld{// CODE}
Within the contract, we define the data type for storing the value. As the Hello World! is the string we proceed with the keyword string. There is various data type such as boolean, integers, address, string, etc. After string, we can define the visibility quantifiers. There are four of these, that is, external, internal, public, and private. You can know it better here. We are going to use the public.
Now define the name of the variable and assign the value to it.
string public myString = "Hello World!";
All together, the code will look like this:
// SPDX-License-Identifier: MITpragma solidity >=0.7.0 <0.9.0;contract hello{string public myString = "Hello World!";}
Learning solidity and mastering it will help you in your career. Solidity for Web3 is like JavaScript for Web Development. In the future, there will be high demand for solidity developers for creating and managing smart contracts.