github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/appctx/appctx.go (about) 1 package appctx 2 3 import ( 4 "context" 5 "strconv" 6 ) 7 8 // Read more: https://blog.golang.org/context#TOC_3.2. 9 // The key type is unexported to prevent collisions with context keys defined in 10 // other packages. 11 type key int 12 13 func (k key) String() string { 14 return strconv.Itoa(int(k)) 15 } 16 17 // Context key constants for responses 18 // Its value of zero is arbitrary. If this package defined other context keys, they would have 19 // different integer values. 20 const ( 21 MetaContextKey key = iota + 1 22 DataContextKey 23 ErrorContextKey 24 TransactionContextKey 25 ) 26 27 // SetValue wrapped the context.WithValue with appctx keys 28 func SetValue(ctx context.Context, key key, value interface{}) context.Context { 29 return context.WithValue(ctx, key, value) 30 } 31 32 // GetValue from app context with key 33 func GetValue(ctx context.Context, key key) interface{} { 34 return ctx.Value(key) 35 }