Context on one-shot structs

104 views
Skip to first unread message

Jean de Klerk

unread,
Jan 12, 2021, 12:27:47 PM1/12/21
to golang-nuts
Hi all,

I'm looking into how contexts are used, and wondering: does anybody know of an idiomatic use of context on a struct for the sake of a one-shot?

(a one-shot being some struct that gets used once, like an RPC or http request)

For example, http.Request has a context on its struct. But, I think the context exists there for backwards compatibility reasons.

I'm looking for a struct that has context on it specifically because the context is scoped to the lifetime of the request.

Thanks!
Jean

Jean de Klerk

unread,
Jan 12, 2021, 12:29:39 PM1/12/21
to golang-nuts
Er, lifetime of the struct *

Ross Light

unread,
Jan 15, 2021, 6:44:58 PM1/15/21
to golang-nuts
The Go CDK stores a Context in a struct while performing I/O: https://pkg.go.dev/gocloud.dev/blob#Bucket.NewWriter

It could be argued that this is done for compatibility with the io.Reader and io.Writer interfaces. However, I think this pattern, used sparingly, is suitable for API interactions where multiple method calls are required for a single conceptual task.

-Ross
Reply all
Reply to author
Forward
0 new messages