Similar to Golang, variables can be assigned with names and data types.
package demo
func Hello(name string) string {
return "Hello " + name + "!"
}
Note: Although concrete data types of Golang are supported, the
avl
package must be used sincemap
is unusable due to its non-deterministic properties.
Data handling in Gnolang works similarly to data handling in Golang.
package demo
var total int
func init() {
total = 1000
}
func Add(nb int) int {
total += nb
return total
}
The code above globally declares an integer-typed total
variable and sets its value to 1000
. Then it changes its value using the Add()
function, by adding the number passed as the argument.
Importing in Gnolang also works similarly to importing in Golang.
package demo
import (
"gno.land/p/demo/avl"
"gno.land/p/demo/dom"
)
func Render(path string) string {
thread := dom.Plot{Name: "Hello!"}
thread.AddPost("Foo", "foo foo foo")
thread.AddPost("Bar", "bar bar bar")
return thread.String()
}
The sample code above imports the avl
and dom
packages by their respective package paths.
In Gnolang, the distinction of access modifiers is based on the naming conventions of objects.
- Starting with an uppercase
- Can be externally accessed, representing:
Public
- Similar to the
public
andexternal
modifiers in Solidity
- Can be externally accessed, representing:
- Starting with a lowercase
- Cannot be externally accessed, representing:
Private
- Similar to the
private
andinternal
modifiers in Solidity
- Cannot be externally accessed, representing: