Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

counter.gno

0.82 Kb · 37 lines
 1package governance
 2
 3// Counter manages unique incrementing IDs.
 4type Counter struct {
 5	id int64
 6}
 7
 8// NewCounter creates a new Counter whose current ID is initialized to zero.
 9//
10// Returns:
11//   - *Counter: counter initialized with current ID 0
12func NewCounter() *Counter {
13	return &Counter{
14		id: 0,
15	}
16}
17
18// Get returns the counter's current ID without changing it.
19//
20// Returns:
21//   - int64: currently stored counter ID
22func (c *Counter) Get() int64 { return c.id }
23
24// Set replaces the counter's current ID with id.
25//
26// Parameters:
27//   - id: new current ID to store; the value is not incremented
28func (c *Counter) Set(id int64) { c.id = id }
29
30// Next increments the counter and returns the resulting ID.
31//
32// Returns:
33//   - int64: incremented counter ID after the update
34func (c *Counter) Next() int64 {
35	c.id++
36	return c.id
37}