func foo(c context.Context) { // do something with context } func bar(c context.Context) { foo(c) } func main() { ctx := context.Background() bar(ctx) }
func handleRequest(w http.ResponseWriter, r *http.Request) { ctx := appengine.NewContext(r) ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() // perform lengthy operation // ... // check if context deadline exceeded if ctx.Err() == context.DeadlineExceeded { http.Error(w, "Request timed out", http.StatusRequestTimeout) return } // respond to client // ... }In this example, we use the `WithTimeout` function to set a 5-second timeout on the request context. We then use a `defer` statement to call the `cancel` function, which is used to stop any ongoing operations if the timeout is reached. Finally, we check if the context deadline was exceeded and respond appropriately to the client. Overall, Go appengine Context is a powerful package library that makes it easy to manage context and state in Google App Engine applications. It provides a set of functions and methods for handling timeouts, cancellations, and error handling.