On Fri, Apr 14, 2017 at 3:34 PM, Ramachandran Gurumoorthy
<ram...@gmail.com> wrote:
>
> I am working on a piece of code that imports "context". All the APIs include
> (foobar context.Context) variable.
>
> Another imported package expects "golang.org/x/net/context".Context as an
> argument to one of its API.  When I try to supply foobar to the api
> expecting ("golang.org/x/net/context".Context), I get a compiler error.
>
> cannot use &ctxt (type *"context".Context) as type
> *"golang.org/x/net/context".Context in argument to
> "gopkg.in/spacemonkeygo/monitor.v1/trace".Trace:
> *"golang.org/x/net/context".Context is pointer to interface, not interface
>
> Question: Is there a way to cast context.Context to
> "golang.org/x/net/context".Context?

In 1.9, type aliases will address this problem.  In 1.8 and earlier,
no, there is no way.

That said, it's odd to pass a *context.Context.  A context.Context is
an interface type, and can normally  be passed by value.  If the API
genuinely does require a pointer, then you can workaround the problem
by writing something along the lines of

import oldcontext "golang.org/x/net/context"

    var oldctxt oldcontext.Context = ctxt
    trace.F(&oldctxt)
    ctxt = oldctxt

Since the two different Context types are both interface types with
the same methods, they are assignment-compatible.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to