Counter
This Solidity contract defines a counter that can be incremented or decremented by anyone.
Solidity Version
1contract Counter {
2 int public count = 0;
3
4 function increment() public {
5 count++;
6 }
7
8 function decrement() public {
9 count--;
10 }
11}
Explanation
This Solidity contract does:
- Declare a
countvariable readable by anyone initialized at 0 - Define two functions,
incrementanddecrement, that increase and decrease the counter by 1. - These functions are marked
public, meaning anyone can call them
Gno Version
1package counter
2
3var count int64 = 0
4
5func Increment(cur realm) {
6 count++
7}
8
9func Decrement(cur realm) {
10 count--
11}
- We define a global variable
countof typeint64. Since it is declared at the package level, its value is persistent, just like in Solidity. As it is capitalized, it can be accessed from outside the package (read-only). - We provide two functions:
IncrementandDecrement, which modify the counter value using Go-style syntax (count++andcount--). As they are capitalized, they can be called from outside the package.
Live Demo
Counter Value : 0