-2

So I have been searching and can't seem to find how to get the backend struct for a wrapped struct in go.

This is the use case: I am using traffic to manage my web app and it uses it's own wrapped version of the http.Request as well as several others. the declaration looks like this:

type Request struct {
    *http.Request
}

I am trying to incorporate go-guardian and I need to send an http.Request to this function:

    Authenticate(r *http.Request) (Info, error)

The question is how do I get the *http.Request that the traffic.Request was made out of?

I seem to remember seeing a way to do this in a tutorial somewhere but I haven't been able to find it (the problem is I'm not sure I'm using the right term for a wrapped struct).

Any feedback would be graetly appreciated - thank you.

Jim Hessin
  • 137
  • 2
  • 11
  • 3
    From the [spec](https://golang.org/ref/spec#Struct_types): `The unqualified type name acts as the field name` – JimB Jun 01 '21 at 19:45

1 Answers1

1

An embedded field can be accessed using its type name:

type Request struct {
    *http.Request
}

For the above:

func f(r *Request) {
  // This will pass the embedded *http.Request
  g(r.Request)
}
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • 1
    I kept trying `r.(http.Request)` `http.Request(r)` and many variations involving a moving (*) - this worked though. Thank you! – Jim Hessin Jun 01 '21 at 20:40