Skip to content

Commit 72d033e

Browse files
committed
Fix undefined requestIDKey compilation error
Move contextKey type and requestIDKey constant to package level scope so they can be accessed from multiple functions in the advanced example. Remove duplicate definitions inside functions.
1 parent 07e1641 commit 72d033e

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

advanced

11.6 MB
Binary file not shown.

examples/advanced/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
"github.com/vnykmshr/obcache-go/pkg/obcache"
1111
)
1212

13+
// Context key type for type safety
14+
type contextKey string
15+
16+
const requestIDKey contextKey = "requestID"
17+
1318
// User represents a user from the database
1419
type User struct {
1520
ID int `json:"id"`
@@ -188,8 +193,6 @@ func main() {
188193
cachedGetUserWithContext := obcache.Wrap(cache, userService.GetUserWithContext)
189194

190195
// Create contexts with request metadata
191-
type contextKey string
192-
const requestIDKey contextKey = "requestID"
193196
ctx1 := context.WithValue(context.Background(), requestIDKey, "req-001")
194197
ctx2 := context.WithValue(context.Background(), requestIDKey, "req-002")
195198

0 commit comments

Comments
 (0)