Closed berupp closed 7 years ago
Interesting. I just tried to reproduce this and couldn't. I copied your code verbatim (btw thanks, this made it trivial to reproduce!) and got this:
$ go run main.go
Matcher Type: *context.valueCtx - Param Type: *context.valueCtx -> matches true
Shouldn't be nil &{<nil>}
To make that last line more readable, I changed it to use https://github.com/davecgh/go-spew:
func NewEndpoint(client SomeClient) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
clientWithContext := client.WithContext(r.Context())
spew.Dump(clientWithContext)
}
}
That gives me:
$ go run main.go
Matcher Type: *context.valueCtx - Param Type: *context.valueCtx -> matches true
(*main.MockSomeClient)(0xc42009c040)({
fail: (func(string, ...int)) <nil>
})
I'm using
$ go version
go version go1.7.3 linux/amd64
Is it possible that you're using a different go version?
Hi Peter Thanks for answering so quickly. I am puzzled. I ran the original tests on OS/X so, now I tried it on a linux VM but I am getting the same failure result.
-bash-4.2$ go run main.go
O: *context.valueCtx, P: *context.valueCtx - match false
(interface {}) <nil>
-bash-4.2$ go version
go version go1.7.3 linux/amd64
-bash-4.2$
I'm asking my coworkers to run it on their machines.
Okay so my coworkers ran it and get the same result as you. I set up go on my Windows machine and I also get the expected, correct result.
So something really odd seems to be going on with my Mac...anyway, closing.
Thanks for your help
I am building a REST endpoint using
gorilla/mux
. I am passing an object into my http.Handlerfunc which will get enriched with the request's context when the endpoint is called.Now the code sample at the bottom reproduces the issue. I also added a Println to matcher.go:
The output, when I run the sample code below, is as follows:
What I do not understand is: The types are the same, but
AssignableTo
returns false. I guess I am making a wrong assumption aboutreflect.TypeOf
because my understanding is: If Ireflect.TypeOf
and object, the type should be completely independent from the content of the object. So why would the code fail? I am clueless.The sample code: