Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/hello/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186410 max_deposit = "5000000ugnot"
hello.gno
// Package hello is a canary realm: it proves the end-to-end pipeline (workspace
// resolution of a versioned local dependency, gno test, gno lint, and CI)
// works from the very first commit. It has no external dependencies. Remove it
// once real realms land.
package hello
import (
"strings"
"gno.land/p/nym-moultest123/greet/v0"
)
// greets counts how many times Greet has been called (persistent state).
var greets int
// Greet records a greeting from the caller and returns it. Crossing function:
// it mutates persistent state, so it takes `cur realm` per the gno 0.9
// interrealm convention.
func Greet(cur realm, name string) string {
greets++
return greet.Greet(name)
}
// Count returns how many greetings have been recorded. Read-only.
func Count() int { return greets }
// Render is the gnoweb Markdown view.
func Render(path string) string {
var b strings.Builder
b.WriteString("# r/moul/hello/v0\n\n")
b.WriteString("Canary realm for the gno-contracts repository.\n\n")
b.WriteString("- Greetings recorded: ")
b.WriteString(itoa(greets))
b.WriteString("\n")
return b.String()
}
// itoa avoids importing strconv for a single conversion in the canary.
func itoa(n int) string {
if n == 0 {
return "0"
}
neg := n < 0
if neg {
n = -n
}
var buf [20]byte
i := len(buf)
for n > 0 {
i--
buf[i] = byte('0' + n%10)
n /= 10
}
if neg {
i--
buf[i] = '-'
}
return string(buf[i:])
}
hello_test.gno
package hello
import (
"strings"
"testing"
)
func TestGreetAndCount(cur realm, t *testing.T) {
if got := Count(); got != 0 {
t.Fatalf("initial count = %d, want 0", got)
}
if got := Greet(cross(cur), "moul"); got != "Hello, moul!" {
t.Errorf("Greet = %q, want %q", got, "Hello, moul!")
}
Greet(cross(cur), "")
if got := Count(); got != 2 {
t.Errorf("count = %d, want 2", got)
}
}
func TestRender(t *testing.T) {
out := Render("")
if !strings.Contains(out, "r/moul/hello/v0") {
t.Errorf("render missing header: %q", out)
}
}