Skip to content

Commit f75a77a

Browse files
authored
chore(examples): add microposts example (#3660)
Signed-off-by: moul <[email protected]>
1 parent b4ebf6c commit f75a77a

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# fork of `leon/fosdem25/microposts`
2+
3+
removing optional lines to make the code more concise for slides.
4+
5+
Original work here: https://gno.land/r/leon/fosdem25/microposts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module gno.land/r/moul/microposts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package microposts
2+
3+
// empty file just to make sure that `gno test` tries to parse the implementation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package microposts
2+
3+
import (
4+
"std"
5+
"time"
6+
)
7+
8+
type Post struct {
9+
text string
10+
author std.Address
11+
createdAt time.Time
12+
}
13+
14+
func (p Post) String() string {
15+
out := p.text + "\n"
16+
out += "_" + p.createdAt.Format("02 Jan 2006, 15:04") + ", by " + p.author.String() + "_"
17+
return out
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package microposts
2+
3+
import (
4+
"std"
5+
"strconv"
6+
"time"
7+
)
8+
9+
var posts []*Post
10+
11+
func CreatePost(text string) {
12+
posts = append(posts, &Post{
13+
text: text,
14+
author: std.PrevRealm().Addr(), // provided by env
15+
createdAt: time.Now(),
16+
})
17+
}
18+
19+
func Render(_ string) string {
20+
out := "# Posts\n"
21+
for i := len(posts) - 1; i >= 0; i-- {
22+
out += "### Post " + strconv.Itoa(i) + "\n" + posts[i].String()
23+
}
24+
return out
25+
}

0 commit comments

Comments
 (0)