如何通过不同的结构发挥作用?

I have several different structures.

Here show two:

type AdsResponse struct {
    Body struct {
        Docs []struct {
            ID        int  `json:"ID"`
            // others

        } `json:"docs"`
    } `json:"response"`
    Header `json:"responseHeader"`
}

type OtherResponse struct {
    Body struct {
        Docs []struct {
            ID    int     `json:"ID"`
            // others
        } `json:"docs"`
    } `json:"response"`
    Header `json:"responseHeader"`
}

but i don't know how i can do for this method accepts and return both.

func Get(url string, response Response) (Response, bool) {

    res, err := goreq.Request{
        Uri:         url,
    }.Do()

    // several validations

    res.Body.FromJsonTo(&response)

    return response, true
}

And use like this:

var struct1 AdsResponse
var struct2 OtherResponse

Get("someURL", struct1)
Get("someURL", struct2)

There are any form?

Your code example is somewhat confusing since both structs appear to be identical. I'll assume that they differ somewhere in "others".

First, I generally recommend creating a wrapper around these kinds of JSON deserializations. Working directly on the JSON structure is fragile. Most of your program should not be aware of the fact that the data comes down in JSON. So for instance, you can wrap this in an Ads struct that contains an AdsResponse, or just copies the pieces it cares about out of it. Doing that will also make some of the below slightly easier to implement and less fragile.

The most common solution is probably to create an interface:

type Response interface {
    ID() int
}

You make both Ads and Others conform to Response. Then you can return Response. If necessary, you can type-switch later to figure out which one you have and unload other data.

switch response := response.(type) {
case Ads:
    ...
case Other:
    ...
}

I don't quite get why you have the reponse as a parameter and as a return. I think you dont need to return it. You should pass a pointer to the reponse and fill it with the data. Also, I'd return an Error instead of a boolean, but that is another topic.

Anyway, the solution is to use interface{} (empty interface).

You are lucky because the function you are using (FromJsonTo) accepts an empty interface as a parameter, so you can safely change your parameter type to interface{} and just pass it to FromJsonTo. Like this:

func Get(url string, response interface{}) bool {

    res, err := goreq.Request{
        Uri:         url,
    }.Do()

    // several validations

    res.Body.FromJsonTo(response)

    return true
}

Warning: I did not compile the code.

Then you would call this function with the url and a pointer to one of the reponse structs like this:

var struct1 AdsResponse
var struct2 OtherResponse

Get("someURL", &struct1)
Get("someURL", &struct2)

Follow the solution working at Go Playground

Go has no polymorphic or any other OO like behaviour, so, when you try to pass a AdsResponse or OtherResponse struct as an Response (or any interface{}), these values becomes an Response (or other param type specified), and is not possible to Go to infer the real type that originate these interface{} and correctly decode your json to these struct types as expected.

This kind of thing should works perfectly in OO languages, like Java, C# etc. There is no hierarchy generalization/specialization on structs/interfaces in Go.

You would need to do a type assertion in your Rest executor, or a switch case, but it seems that you need a generic REST executor, like a generic lib some thing like that. Would not reasonable create a switch case for each struct in your program. Maybe you have dozens or hundreds of structs soon.

I think that a reasonable solution is the rest client pass a lambda function to do the last step for your, that is just create a correct struct destination type and call json decode.

As i say above, the return type of executeRest() in my example will became an interface{}, but the rest client can securely do the type assertion of returned value after executeRest() call.

The way to achieve this is through Go's interfaces.

Two options:

empty interface

Get(url string, response interface{}) (Response, bool)

This option allows any value to be given to this function.

custom interface

Creating a custom interface will allow you to narrow down the types that can be provided as arguments to your function.

In this case you'll have to create an interface that all your Response structs will need to abide by. Any struct really that abides by that interface will be able to be used as an argument of your function.

Something like this:

type MyResponse interface {
    SomeFunction()
} 

Then your function signature could look like

Get(url string, response MyResponse) (MyResponse, bool)

As long as AdsResponse and OtherResponse abide by the MyResponse interface, they will be allowed to be used as arguments to the function.