Skip to content

Commit

Permalink
Merge pull request #851 from devlights/add-ring-index-example
Browse files Browse the repository at this point in the history
  • Loading branch information
devlights authored Sep 20, 2024
2 parents 2a271c3 + 2163c9b commit c907d45
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/singleapp/ring_index/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://taskfile.dev

version: '3'

tasks:
default:
cmds:
- go run .
57 changes: 57 additions & 0 deletions examples/singleapp/ring_index/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"fmt"
"log"
)

type (
ring struct {
curr int
last int
next int
}
)

func newRing() *ring {
return &ring{curr: 0, last: 2, next: 1}
}

func (me *ring) rotate() {
me.curr = (me.curr + 1) % 3
me.last = (me.last + 1) % 3
me.next = (me.next + 1) % 3
}

func (me *ring) String() string {
return fmt.Sprintf("(curr=%d,last=%d,next=%d)", me.curr, me.last, me.next)
}

func main() {
log.SetFlags(0)

ctx := context.Background()
if err := run(ctx); err != nil {
log.Panic(err)
}
}

func run(ctx context.Context) error {
var (
r = newRing()
)

for range 10 {
select {
case <-ctx.Done():
return ctx.Err()
default:
}

log.Println(r)
r.rotate()
}

return nil
}

0 comments on commit c907d45

Please sign in to comment.