-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstateful_goroutines.go
More file actions
40 lines (33 loc) · 1.04 KB
/
Copy pathstateful_goroutines.go
File metadata and controls
40 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package stateful_goroutines
// TODO:
// - Implement a Counter that manages state using a single goroutine and channels.
// - The counter should support Increment and GetValue operations.
// - State must be owned by a single goroutine to avoid race conditions.
// - Other goroutines communicate via channels to read or modify the state.
// readOp represents a read request
type readOp struct {
resp chan int
}
// writeOp represents a write request (increment)
type writeOp struct {
amount int
resp chan bool
}
type Counter struct {
reads chan readOp
writes chan writeOp
}
// NewCounter creates and starts a new stateful counter
func NewCounter() *Counter {
// TODO: initialize channels and start the state-owning goroutine
return &Counter{}
}
// Increment increments the counter by the given amount
func (c *Counter) Increment(amount int) {
// TODO: send a write operation and wait for confirmation
}
// GetValue returns the current counter value
func (c *Counter) GetValue() int {
// TODO: send a read operation and return the value
return 0
}