-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #851 from devlights/add-ring-index-example
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# https://taskfile.dev | ||
|
||
version: '3' | ||
|
||
tasks: | ||
default: | ||
cmds: | ||
- go run . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |