From the course: Build an Ethereum Smart Contract with Go and Solidity
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Constructor - Go Tutorial
From the course: Build an Ethereum Smart Contract with Go and Solidity
Constructor
- [Instructor] A constructor is a special function in a contract that is executed only once. at the time when the contract is created. You declare a constructor with the constructor keyword. Like other functions, a constructor can have parameters and can be declared as public or internal. However, unlike other functions, constructors cannot be external or private. In this example, the constructor takes one per duration. When the contract is deployed, duration is set as the initial value of the state variable duration. It's important to note that the constructors are code only once and cannot be code again after the contract is deployed. This makes them perfect place to put any setup or initialization logic for your contract. Let's say you want to set another state variable owner of the contract at the time of the deployment. Let's say how we can do it. First of all, we can define a owner state variable, which is address, and then within the constructor, let's assign the sender of the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.