ctx := context.Background() // Cancelling the context ctx, cancel := context.WithCancel(ctx)
ctx, cancel := context.WithTimeout(ctx, 5 * time.Second)
type contextKey string func setValueToContext(ctx context.Context, value string) context.Context { return context.WithValue(ctx, contextKey("foo"), value) } func getValueFromContext(ctx context.Context) (string, error) { value, ok := ctx.Value(contextKey("foo")).(string) if !ok { return "", errors.New("foo key not found in context") } return value, nil }These are examples of using the Context package in a Go program to control context over an API.